Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 21 | class PaginateElasticaQuerySubscriber implements EventSubscriberInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var RequestStack |
||
| 25 | */ |
||
| 26 | private $requestStack; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param RequestStack $requestStack |
||
| 30 | */ |
||
| 31 | 9 | public function __construct(RequestStack $requestStack) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * @param ItemsEvent $event |
||
| 38 | */ |
||
| 39 | 9 | public function items(ItemsEvent $event) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | 1 | public static function getSubscribedEvents() |
|
| 63 | { |
||
| 64 | return [ |
||
| 65 | 1 | 'knp_pager.items' => ['items', 1], |
|
| 66 | 1 | ]; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Adds knp paging sort to query. |
||
| 71 | * |
||
| 72 | * @param ItemsEvent $event |
||
| 73 | */ |
||
| 74 | 9 | protected function setSorting(ItemsEvent $event) |
|
| 75 | { |
||
| 76 | 9 | $options = $event->options; |
|
| 77 | 9 | $sortField = $this->getRequest()->get($options['sortFieldParameterName']); |
|
| 78 | |||
| 79 | 9 | if (!$sortField && isset($options['defaultSortFieldName'])) { |
|
| 80 | 1 | $sortField = $options['defaultSortFieldName']; |
|
| 81 | 1 | } |
|
| 82 | |||
| 83 | 9 | if (!empty($sortField)) { |
|
| 84 | 8 | $event->target->getQuery()->setSort([ |
|
| 85 | 8 | $sortField => $this->getSort($sortField, $options), |
|
| 86 | 7 | ]); |
|
| 87 | 7 | } |
|
| 88 | 8 | } |
|
| 89 | |||
| 90 | 8 | protected function getSort($sortField, array $options = []) |
|
| 91 | { |
||
| 92 | $sort = [ |
||
| 93 | 8 | 'order' => $this->getSortDirection($sortField, $options), |
|
| 94 | 7 | ]; |
|
| 95 | |||
| 96 | 7 | View Code Duplication | if (isset($options['sortNestedPath'])) { |
| 97 | 4 | $path = is_callable($options['sortNestedPath']) ? |
|
| 98 | 4 | $options['sortNestedPath']($sortField) : $options['sortNestedPath']; |
|
| 99 | |||
| 100 | 4 | if (!empty($path)) { |
|
| 101 | 4 | $sort['nested_path'] = $path; |
|
| 102 | 4 | } |
|
| 103 | 4 | } |
|
| 104 | |||
| 105 | 7 | View Code Duplication | if (isset($options['sortNestedFilter'])) { |
| 106 | 2 | $filter = is_callable($options['sortNestedFilter']) ? |
|
| 107 | 2 | $options['sortNestedFilter']($sortField) : $options['sortNestedFilter']; |
|
| 108 | |||
| 109 | 2 | if (!empty($filter)) { |
|
| 110 | 2 | $sort['nested_filter'] = $filter; |
|
| 111 | 2 | } |
|
| 112 | 2 | } |
|
| 113 | |||
| 114 | 7 | return $sort; |
|
| 115 | } |
||
| 116 | |||
| 117 | 8 | protected function getSortDirection($sortField, array $options = []) |
|
| 118 | { |
||
| 119 | 8 | $dir = 'asc'; |
|
| 120 | 8 | $sortDirection = $this->getRequest()->get($options['sortDirectionParameterName']); |
|
| 121 | |||
| 122 | 8 | if (empty($sortDirection) && isset($options['defaultSortDirection'])) { |
|
| 123 | $sortDirection = $options['defaultSortDirection']; |
||
| 124 | } |
||
| 125 | |||
| 126 | 8 | if ('desc' === strtolower($sortDirection)) { |
|
| 127 | 1 | $dir = 'desc'; |
|
| 128 | 1 | } |
|
| 129 | |||
| 130 | // check if the requested sort field is in the sort whitelist |
||
| 131 | 8 | if (isset($options['sortFieldWhitelist']) && !in_array($sortField, $options['sortFieldWhitelist'])) { |
|
| 132 | 1 | throw new \UnexpectedValueException(sprintf('Cannot sort by: [%s] this field is not in whitelist', $sortField)); |
|
| 133 | } |
||
| 134 | |||
| 135 | 7 | return $dir; |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return Request|null |
||
| 140 | */ |
||
| 141 | 9 | private function getRequest() |
|
| 145 | } |
||
| 146 |