Total Complexity | 45 |
Total Lines | 183 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
Complex classes like FormHandler 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 FormHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class FormHandler |
||
15 | { |
||
16 | private Bpost $bpost; |
||
17 | |||
18 | /** @var array<string,mixed> */ |
||
19 | private array $parameters = []; |
||
20 | |||
21 | /** |
||
22 | * Create bPostFormHandler instance |
||
23 | */ |
||
24 | public function __construct(string $accountId, string $passPhrase, string $apiUrl = Bpost::API_URL) |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Calculate the hash |
||
31 | */ |
||
32 | private function getChecksum(): string |
||
33 | { |
||
34 | $keysToHash = [ |
||
35 | 'accountId', |
||
36 | 'action', |
||
37 | 'costCenter', |
||
38 | 'customerCountry', |
||
39 | 'deliveryMethodOverrides', |
||
40 | 'extraSecure', |
||
41 | 'orderReference', |
||
42 | 'orderWeight', |
||
43 | ]; |
||
44 | |||
45 | $base = 'accountId=' . $this->bpost->getAccountId() . '&'; |
||
46 | |||
47 | foreach ($keysToHash as $key) { |
||
48 | if (!array_key_exists($key, $this->parameters)) { |
||
49 | continue; |
||
50 | } |
||
51 | |||
52 | $value = $this->parameters[$key]; |
||
53 | |||
54 | if (!is_array($value)) { |
||
55 | $base .= $key . '=' . $value . '&'; |
||
56 | continue; |
||
57 | } |
||
58 | |||
59 | // Si c’est un tableau, concaténer chaque entrée (tri déjà fait dans setParameter) |
||
60 | foreach ($value as $entry) { |
||
61 | $base .= $key . '=' . $entry . '&'; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | // add passphrase |
||
66 | $base .= $this->bpost->getPassPhrase(); |
||
67 | |||
68 | return hash('sha256', $base); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get the parameters |
||
73 | * |
||
74 | * @return array<string,mixed> |
||
75 | */ |
||
76 | public function getParameters(bool $form = false, bool $includeChecksum = true): array |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Set a parameter |
||
97 | * |
||
98 | * @throws BpostInvalidValueException |
||
99 | * @throws BpostInvalidLengthException |
||
100 | */ |
||
101 | public function setParameter(string $key, mixed $value): void |
||
200 |