1 | <?php |
||
22 | class ConsoleController extends AbstractActionController |
||
23 | { |
||
24 | /** |
||
25 | * @var Console |
||
26 | */ |
||
27 | protected $console; |
||
28 | |||
29 | /** |
||
30 | * Is quiet mode enabled? |
||
31 | * |
||
32 | * @var bool |
||
33 | */ |
||
34 | protected $isQuiet; |
||
35 | |||
36 | /** |
||
37 | * @var DatabaseConfig |
||
38 | */ |
||
39 | protected $config; |
||
40 | |||
41 | /** |
||
42 | * @var Client |
||
43 | */ |
||
44 | protected $httpClient; |
||
45 | |||
46 | /** |
||
47 | * @param Console $console |
||
48 | * @param DatabaseConfig $config |
||
49 | * @param Client $httpClient |
||
50 | */ |
||
51 | public function __construct(Console $console, DatabaseConfig $config, Client $httpClient) |
||
52 | { |
||
53 | $this->console = $console; |
||
54 | $this->config = $config; |
||
55 | $this->setHttpClient($httpClient); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param Client $httpClient |
||
60 | */ |
||
61 | public function setHttpClient(Client $httpClient) |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function dispatch(RequestInterface $request, ResponseInterface $response = null) |
||
76 | |||
77 | /** |
||
78 | * Download GeoIP data via console |
||
79 | */ |
||
80 | public function downloadAction() |
||
81 | { |
||
82 | $datFilePath = $this->config->getDatabasePath(); |
||
83 | $events = $this->getEventManager(); |
||
84 | |||
85 | if ($this->getRequest()->getParam('no-clobber') && is_file($datFilePath)) { |
||
|
|||
86 | $events->trigger(__FUNCTION__ . '.exists', $this, [ |
||
87 | 'path' => $datFilePath, |
||
88 | ]); |
||
89 | $this->writeLine('Database already exist. Skipping...'); |
||
90 | return; |
||
91 | } |
||
92 | |||
93 | try { |
||
94 | $response = $this->getDbResponse(); |
||
95 | } catch (RuntimeException $e) { |
||
96 | $this->writeLineError(sprintf('%s', $e->getMessage())); |
||
97 | return; |
||
98 | } |
||
99 | |||
100 | if (!$response instanceof Response || $response->getStatusCode() !== Response::STATUS_CODE_200) { |
||
101 | $this->writeLineError('Error during file download occured'); |
||
102 | return; |
||
103 | } |
||
104 | |||
105 | $events->trigger(__FUNCTION__ . '.pre', $this, [ |
||
106 | 'path' => $datFilePath, |
||
107 | 'response' => $response, |
||
108 | ]); |
||
109 | |||
110 | $this->writeLineSuccess('Download completed'); |
||
111 | $this->writeLine('Unzip the downloading data...'); |
||
112 | |||
113 | file_put_contents($datFilePath, gzdecode($response->getBody())); |
||
114 | |||
115 | $events->trigger(__FUNCTION__ . '.post', $this, [ |
||
116 | 'path' => $datFilePath, |
||
117 | ]); |
||
118 | |||
119 | $this->writeLineSuccess(sprintf('Unzip completed (%s)', $datFilePath)); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return Response |
||
124 | */ |
||
125 | public function getDbResponse() |
||
126 | { |
||
127 | $source = $this->config->getSource(); |
||
128 | |||
129 | $this->writeLine(sprintf('Downloading %s...', $source)); |
||
130 | |||
131 | $this->httpClient->setUri($source); |
||
132 | $this->httpClient->setMethod(Request::METHOD_GET); |
||
133 | |||
134 | return $this->httpClient->send(); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param string $text |
||
139 | * @param int $color |
||
140 | * @param int $bgColor |
||
141 | */ |
||
142 | public function writeLine($text, $color = null, $bgColor = null) |
||
148 | |||
149 | /** |
||
150 | * @param string $text |
||
151 | */ |
||
152 | public function writeLineError($text) |
||
156 | |||
157 | /** |
||
158 | * @param string $text |
||
159 | */ |
||
160 | public function writeLineSuccess($text) |
||
164 | |||
165 | /** |
||
166 | * @return Console |
||
167 | */ |
||
168 | public function getConsole() |
||
172 | |||
173 | /** |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function isQuietMode() |
||
183 | } |
||
184 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: