partkeepr

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

GetPreferencesAction.php (1720B)


      1 <?php
      2 
      3 namespace PartKeepr\SystemPreferenceBundle\Action;
      4 
      5 use Dunglas\ApiBundle\Action\ActionUtilTrait;
      6 use Dunglas\ApiBundle\Api\ResourceInterface;
      7 use Dunglas\ApiBundle\Exception\RuntimeException;
      8 use PartKeepr\CategoryBundle\Exception\RootNodeNotFoundException;
      9 use PartKeepr\SystemPreferenceBundle\Service\SystemPreferenceService;
     10 use Symfony\Component\HttpFoundation\JsonResponse;
     11 use Symfony\Component\HttpFoundation\Request;
     12 use Symfony\Component\Serializer\Serializer;
     13 
     14 /**
     15  * Returns the tree root node.
     16  */
     17 class GetPreferencesAction
     18 {
     19     use ActionUtilTrait;
     20 
     21     /**
     22      * @var SystemPreferenceService
     23      */
     24     private $systemPreferenceService;
     25 
     26     /**
     27      * @var Serializer
     28      */
     29     private $serializer;
     30 
     31     public function __construct(
     32         SystemPreferenceService $systemPreferenceService,
     33         Serializer $serializer
     34     ) {
     35         $this->systemPreferenceService = $systemPreferenceService;
     36         $this->serializer = $serializer;
     37     }
     38 
     39     /**
     40      * Retrieves a collection of resources.
     41      *
     42      * @param Request $request
     43      *
     44      * @throws RuntimeException|RootNodeNotFoundException
     45      *
     46      * @return JsonResponse
     47      */
     48     public function __invoke(Request $request)
     49     {
     50         $preferences = $this->systemPreferenceService->getPreferences();
     51 
     52         /**
     53          * @var ResourceInterface
     54          */
     55         list($resourceType) = $this->extractAttributes($request);
     56 
     57         /*
     58          * @var ResourceInterface $resourceType
     59          */
     60         $serializedData = $this->serializer->normalize(
     61             $preferences,
     62             'json',
     63             $resourceType->getNormalizationContext()
     64         );
     65 
     66         return new JsonResponse($serializedData);
     67     }
     68 }