1 | <?php |
||
20 | class GetConfigsAction implements MiddlewareInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var EntityManagerInterface |
||
24 | */ |
||
25 | private $entityManager; |
||
26 | /** |
||
27 | * @var LoggerInterface |
||
28 | */ |
||
29 | private $logger; |
||
30 | /** |
||
31 | * @var ExtractionInterface |
||
32 | */ |
||
33 | private $extraction; |
||
34 | |||
35 | 5 | public function __construct( |
|
44 | |||
45 | /** |
||
46 | * @inheritdoc |
||
47 | */ |
||
48 | 5 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
49 | { |
||
50 | 5 | $data = $request->getQueryParams(); |
|
51 | 5 | $page = isset($data['p']) ? abs($data['p']) : null; |
|
52 | 5 | $filters = $data['f'] ?? []; |
|
53 | 5 | $sorting = $data['s'] ?? []; |
|
54 | 5 | $success = false; |
|
55 | 5 | $msg = ''; |
|
56 | 5 | $status = 200; |
|
57 | |||
58 | try { |
||
59 | 5 | $criteria = $this->buildCriteria($filters, $sorting, $page); |
|
60 | |||
61 | /** @var Collection $configs */ |
||
62 | 5 | $configs = $this->entityManager |
|
63 | 5 | ->getRepository(DbConfiguration::class) |
|
64 | 5 | ->matching($criteria); |
|
65 | // before collection load to count all records without pagination |
||
66 | 5 | $count = $configs->count(); |
|
67 | 5 | if ($count > 0) { |
|
68 | 4 | $arrayConfigs = array_map(function ($user) { |
|
69 | 4 | return $this->extraction->extract($user); |
|
70 | 4 | }, $configs->toArray()); |
|
71 | 4 | $success = true; |
|
72 | } else { |
||
73 | 1 | $msg = new DangerMessage('Could not find configurations using given conditions.'); |
|
74 | 1 | $status = 404; |
|
75 | } |
||
76 | } catch (ORMException $exception) { |
||
77 | $this->logger->error((string)$exception); |
||
78 | $msg = new DangerMessage('Could not fetch configs.'); |
||
79 | $status = 400; |
||
80 | } |
||
81 | |||
82 | 5 | return new JsonResponse([ |
|
83 | 5 | 'data' => [ |
|
84 | 5 | 'configurations' => $arrayConfigs ?? [], |
|
85 | 5 | 'count' => $count ?? 0, |
|
86 | ], |
||
87 | 5 | 'success' => $success, |
|
88 | 5 | 'msg' => $msg, |
|
89 | 5 | ], $status); |
|
90 | } |
||
91 | |||
92 | 5 | private function buildCriteria(array $filters = [], array $sorting = [], ?int $page, int $limit = 10): Criteria |
|
111 | } |
||
112 |
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: