Total Complexity | 45 |
Total Lines | 200 |
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 |
||
13 | class FormHandler |
||
14 | { |
||
15 | /** |
||
16 | * bPost instance |
||
17 | * |
||
18 | * @var Bpost |
||
19 | */ |
||
20 | private $bpost; |
||
21 | |||
22 | /** |
||
23 | * The parameters |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private $parameters = array(); |
||
28 | |||
29 | /** |
||
30 | * Create bPostFormHandler instance |
||
31 | * |
||
32 | * @param string $accountId |
||
33 | * @param string $passPhrase |
||
34 | * @param string $apiUrl |
||
35 | */ |
||
36 | public function __construct($accountId, $passPhrase, $apiUrl = Bpost::API_URL) |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Calculate the hash |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | private function getChecksum() |
||
47 | { |
||
48 | $keysToHash = array( |
||
49 | 'accountId', |
||
50 | 'action', |
||
51 | 'costCenter', |
||
52 | 'customerCountry', |
||
53 | 'deliveryMethodOverrides', |
||
54 | 'extraSecure', |
||
55 | 'orderReference', |
||
56 | 'orderWeight', |
||
57 | ); |
||
58 | $base = 'accountId=' . $this->bpost->getAccountId() . '&'; |
||
59 | |||
60 | foreach ($keysToHash as $key) { |
||
61 | if (isset($this->parameters[$key])) { |
||
62 | if (!is_array($this->parameters[$key])) { |
||
63 | $base .= $key . '=' . $this->parameters[$key] . '&'; |
||
64 | } else { |
||
65 | foreach ($this->parameters[$key] as $entry) { |
||
66 | $base .= $key . '=' . $entry . '&'; |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | |||
72 | // add passphrase |
||
73 | $base .= $this->bpost->getPassPhrase(); |
||
74 | |||
75 | // return the hash |
||
76 | return hash('sha256', $base); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Get the parameters |
||
81 | * |
||
82 | * @param bool $form |
||
83 | * @param bool $includeChecksum |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | public function getParameters($form = false, $includeChecksum = true) |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Set a parameter |
||
109 | * |
||
110 | * @param string $key |
||
111 | * @param mixed $value |
||
112 | * |
||
113 | * @throws BpostInvalidValueException |
||
114 | * @throws BpostInvalidLengthException |
||
115 | */ |
||
116 | public function setParameter($key, $value) |
||
216 |