Total Complexity | 42 |
Total Lines | 151 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ProfileHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProfileHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class ProfileHelper |
||
26 | { |
||
27 | public function __construct( |
||
41 | } |
||
42 | |||
43 | public function getDisplayName($userId = null): string |
||
44 | { |
||
45 | $userEntity = $this->findUser($userId); |
||
46 | if (!$userEntity) { |
||
|
|||
47 | throw new \InvalidArgumentException('Invalid userId provided'); |
||
48 | } |
||
49 | |||
50 | $key = $this->prefix . ':' . ProfileConstant::ATTRIBUTE_NAME_DISPLAY_NAME; |
||
51 | if ($userEntity->getAttributes()->containsKey($key)) { |
||
52 | return $userEntity->getAttributes()->get($key)->getValue(); |
||
53 | } |
||
54 | |||
55 | return $userEntity->getUsername(); |
||
56 | } |
||
57 | |||
58 | public function getFullName($userId = null): string |
||
73 | } |
||
74 | |||
75 | public function getProfileUrl($userId = null): string |
||
76 | { |
||
77 | $userEntity = $this->findUser($userId); |
||
78 | if (!$userEntity) { |
||
79 | throw new \InvalidArgumentException('Invalid userId provided'); |
||
80 | } |
||
81 | |||
82 | return $this->router->generate('zikulaprofilebundle_profile_display', ['userId' => $userEntity->getId()]); |
||
83 | } |
||
84 | |||
85 | public function getAvatar($userId = null, array $parameters = []): string |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Finds a certain user based on either it's id or it's name. |
||
130 | * |
||
131 | * @param int|string $userId The user's id or name |
||
132 | */ |
||
133 | private function findUser($userId = null): ?User |
||
134 | { |
||
135 | if (empty($userId)) { |
||
136 | return $this->security->getUser(); |
||
137 | } |
||
138 | |||
139 | if (is_numeric($userId)) { |
||
140 | return $this->userRepository->find($userId); |
||
141 | } |
||
142 | |||
143 | // select user id by user name |
||
144 | $results = $this->userRepository->searchActiveUser(['operator' => '=', 'operand' => $userId], 1); |
||
145 | if (!count($results)) { |
||
146 | return null; |
||
147 | } |
||
148 | |||
149 | return $results[0]; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Checks and updates the avatar image size parameters. |
||
154 | */ |
||
155 | private function squareSize(array $parameters = []): array |
||
176 | } |
||
177 | } |
||
178 |