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 |
||
| 13 | class BaseWidgetContentResolver |
||
|
|
|||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var QueryHelper |
||
| 17 | */ |
||
| 18 | protected $queryHelper; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var EntityManager |
||
| 22 | */ |
||
| 23 | protected $entityManager; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var APIBusinessEntityResolver |
||
| 27 | */ |
||
| 28 | protected $apiResolver; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Get the static content of the widget. |
||
| 32 | * |
||
| 33 | * @param Widget $widget |
||
| 34 | * |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | public function getWidgetStaticContent(Widget $widget) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get the business entity content. |
||
| 56 | * |
||
| 57 | * @param Widget $widget |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function getWidgetBusinessEntityContent(Widget $widget) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Get the content of the widget by the entity linked to it. |
||
| 73 | * |
||
| 74 | * @param Widget $widget |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function getWidgetEntityContent(Widget $widget) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Get the content of the widget for the query mode. |
||
| 91 | * |
||
| 92 | * @param Widget $widget |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function getWidgetQueryContent(Widget $widget) |
||
| 97 | { |
||
| 98 | $parameters = $this->getWidgetStaticContent($widget); |
||
| 99 | |||
| 100 | if (APIBusinessEntity::TYPE === $widget->getBusinessEntity()->getType()) { |
||
| 101 | $entity = $this->apiResolver->getBusinessEntities($widget->getBusinessEntity()); |
||
| 102 | } else { |
||
| 103 | $entity = $this->getWidgetQueryBuilder($widget) |
||
| 104 | ->setMaxResults(1) |
||
| 105 | ->getQuery() |
||
| 106 | ->getOneOrNullResult(); |
||
| 107 | } |
||
| 108 | |||
| 109 | $this->populateParametersWithWidgetFields($widget, $entity, $parameters); |
||
| 110 | |||
| 111 | return $parameters; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get the widget query result. |
||
| 116 | * |
||
| 117 | * @param Widget $widget The widget |
||
| 118 | * |
||
| 119 | * @return \Doctrine\ORM\QueryBuilder The list of entities |
||
| 120 | */ |
||
| 121 | public function getWidgetQueryBuilder(Widget $widget) |
||
| 132 | |||
| 133 | protected function populateParametersWithWidgetFields(Widget $widget, $entity, &$parameters) |
||
| 134 | { |
||
| 135 | $fields = $widget->getFields(); |
||
| 136 | //parse the field |
||
| 137 | foreach ($fields as $widgetField => $field) { |
||
| 138 | //get the value of the field |
||
| 139 | if (null !== $entity) { |
||
| 140 | $attributeValue = null; |
||
| 141 | if (null !== $field) { |
||
| 142 | $accessor = new PropertyAccessor(); |
||
| 143 | $attributeValue = $accessor->getValue($entity, $field); |
||
| 144 | } |
||
| 145 | } else { |
||
| 146 | $attributeValue = $widget->getBusinessEntityName().' -> '.$field; |
||
| 147 | } |
||
| 148 | |||
| 149 | $parameters[$widgetField] = $attributeValue; |
||
| 150 | } |
||
| 151 | |||
| 152 | $widget->setEntity($entity); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param QueryHelper $queryHelper |
||
| 157 | */ |
||
| 158 | public function setQueryHelper(QueryHelper $queryHelper) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param EntityManager $entityManager |
||
| 165 | */ |
||
| 166 | public function setEntityManager(EntityManager $entityManager) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param APIBusinessEntityResolver $resolver |
||
| 173 | */ |
||
| 174 | public function setApiBusinessEntityResolver(APIBusinessEntityResolver $resolver) |
||
| 178 | } |
||
| 179 |