0

I am currently refactoring bits of a medium sized project and encountered the following. (Bit of pseudo code to simplify the example)

class PostRepository {

}

class PostManager {

    /**
     * @var PostRepository
     */
    private $repository;

    public function __construct(PostRepository $repository)
    {
        $this->repository = $repository;
    }

}

class DeletePostCommand {

    /**
     * @var PostManager
     */
    private $postManager;

    /**
     * @var PostRepository
     */
    private $postRepository;

    public function __construct(PostManager $postManager, PostRepository $postRepository)
    {
        $this->postManager = $postManager;
        $this->postRepository = $postRepository;
    }

}

Should this be refactored? Or is it fine as it is?

Or should I create a getPostRepository function in my PostManager class? Wouldn't that go against the idea of the Single Responsibility Principle?

Machiel
  • 1,495
  • 12
  • 15
  • For what is responsible `PostManager`? – NHG Feb 13 '14 at 12:53
  • It's responsible for creating, deleting and updating Post objects, which may involve a bit of Business Logic – Machiel Feb 13 '14 at 12:55
  • 1
    Given that DeletePostCommand is dependent on PostManager which in turn is dependent on PostRepository then I would think adding a getRepository to the manager would suffice. But I would also question the need to expose repository methods to the delete command at all. Seems like the manager would have a deletePost method which the command would call as needed. – Cerad Feb 13 '14 at 13:57
  • I totally agree, I forgot to mention that I need to retrieve several posts by specific criteria. These posts have to be deleted. – Machiel Feb 13 '14 at 14:29

0 Answers0