Services

Creation of a service

Services hold business logic and perform all the operations required for the specific operation.

Services needs to receive plain arguments and never and HTTP request.

Each service method can call other services and repositories, but never a controller.

Just like repositories, services need to implement an interface.

The repositories to be used must be imported through the constructor and must be set as private properties in the class.

class ThreadService implements ThreadServiceInterface
{
    private ThreadRepositoryInterface  $threadRepository;

    public function __construct(
        ThreadRepositoryInterface $threadRepository,
    ) {
        $this->threadRepository = $threadRepository;
    }
    
    public function store(string $name, string $status, string|null $description, string $color, int $channelId)
    {
        return $this->threadRepository->storeThread($name, $status, $description, $color, $channelId);
    }

    public function show(Thread $thread)
    {
        return $thread;
    }

    public function update(Thread $thread, string $name, string $status, string|null $description, string $color, int $channelId)
    {
        return $this->threadRepository->updateThread($thread, $name, $status, $description, $color, $channelId);
    }

    public function destroy(Thread $thread)
    {
        return $this->threadRepository->destroyThread($thread);
    }

    public function edit(Thread $thread)
    {
        return $thread;
    }
```

And this is the interface.

interface ThreadServiceInterface
{
    public function edit(Thread $thread);

    public function store(string $name, string $status, string|null $description, string $color, int $channelId);

    public function show(Thread $thread);

    public function update(Thread $thread, string $name, string $status, string|null $description, string $color, int $channelId);

    public function destroy(Thread $thread);
}

Last updated