partkeepr

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

GetTipHistoryCollectionAction.php (1589B)


      1 <?php
      2 
      3 namespace PartKeepr\TipOfTheDayBundle\Action;
      4 
      5 use Dunglas\ApiBundle\Action\ActionUtilTrait;
      6 use Dunglas\ApiBundle\Exception\RuntimeException;
      7 use Dunglas\ApiBundle\Model\DataProviderInterface;
      8 use PartKeepr\AuthBundle\Services\UserService;
      9 use PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDayHistory;
     10 use Symfony\Component\HttpFoundation\Request;
     11 
     12 class GetTipHistoryCollectionAction
     13 {
     14     use ActionUtilTrait;
     15 
     16     /**
     17      * @var DataProviderInterface
     18      */
     19     private $dataProvider;
     20 
     21     /**
     22      * @var UserService
     23      */
     24     private $userService;
     25 
     26     public function __construct(DataProviderInterface $dataProvider, UserService $userService)
     27     {
     28         $this->dataProvider = $dataProvider;
     29         $this->userService = $userService;
     30     }
     31 
     32     /**
     33      * Retrieves a filtered tip of the day history list. Filters by the currently logged in user by default.
     34      *
     35      * @param Request $request
     36      *
     37      * @throws RuntimeException
     38      *
     39      * @return array|\Dunglas\ApiBundle\Model\PaginatorInterface|\Traversable
     40      */
     41     public function __invoke(Request $request)
     42     {
     43         list($resourceType) = $this->extractAttributes($request);
     44 
     45         $collection = $this->dataProvider->getCollection($resourceType);
     46         $user = $this->userService->getUser();
     47 
     48         $resultCollection = [];
     49 
     50         foreach ($collection as $item) {
     51             /**
     52              * @var TipOfTheDayHistory
     53              */
     54             if ($item->getUser() == $user) {
     55                 $resultCollection[] = $item;
     56             }
     57         }
     58 
     59         return $resultCollection;
     60     }
     61 }