partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

GetRootNodeAction.php (1496B)


      1 <?php
      2 
      3 namespace PartKeepr\CategoryBundle\Action;
      4 
      5 use Dunglas\ApiBundle\Action\ActionUtilTrait;
      6 use Dunglas\ApiBundle\Api\ResourceInterface;
      7 use Dunglas\ApiBundle\Exception\RuntimeException;
      8 use Gedmo\Tree\Entity\Repository\AbstractTreeRepository;
      9 use PartKeepr\CategoryBundle\Exception\RootNodeNotFoundException;
     10 use Symfony\Bridge\Doctrine\ManagerRegistry;
     11 use Symfony\Component\HttpFoundation\Request;
     12 
     13 /**
     14  * Returns the tree root node.
     15  */
     16 class GetRootNodeAction
     17 {
     18     use ActionUtilTrait;
     19 
     20     /**
     21      * @var ManagerRegistry
     22      */
     23     private $manager;
     24 
     25     public function __construct(ManagerRegistry $manager)
     26     {
     27         $this->manager = $manager;
     28     }
     29 
     30     /**
     31      * Retrieves a collection of resources.
     32      *
     33      * @param Request $request
     34      *
     35      * @throws RuntimeException|RootNodeNotFoundException
     36      *
     37      * @return array|\Dunglas\ApiBundle\Model\PaginatorInterface|\Traversable
     38      */
     39     public function __invoke(Request $request)
     40     {
     41         list($resourceType) = $this->extractAttributes($request);
     42 
     43         /*
     44          * @var ResourceInterface $resourceType
     45          */
     46         $repository = $this->manager->getRepository($resourceType->getEntityClass());
     47 
     48         /*
     49          * @var $repository AbstractTreeRepository
     50          */
     51         $rootNodes = $repository->getRootNodes();
     52 
     53         if (count($rootNodes) == 0) {
     54             throw new RootNodeNotFoundException();
     55         }
     56 
     57         $rootNode = reset($rootNodes);
     58 
     59         return $rootNode;
     60     }
     61 }