1
|
|
|
<?php namespace Tequilarapido\Cli\Commands\Base; |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
use Tequilarapido\Cli\Commands\Base\AbstractCommand; |
8
|
|
|
use Tequilarapido\Cli\Config\Config; |
9
|
|
|
use Tequilarapido\Helpers\MailHelper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* All commands that inherit from this class, will take json configuration |
14
|
|
|
* file as first argument. And configuration will be accessible from $config variable. |
15
|
|
|
* |
16
|
|
|
* Class AbstractConfigurableCommand |
17
|
|
|
* @package Tequilarapido\Cli\Commands |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractConfigurableCommand extends AbstractCommand |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \Tequilarapido\Cli\Config\Config |
23
|
|
|
*/ |
24
|
|
|
protected $config = null; |
25
|
|
|
|
26
|
|
|
public function __construct($name = null) |
27
|
|
|
{ |
28
|
|
|
// Check that we have a schema file defined |
29
|
|
|
if (!defined('CLI_SCHEMA_FILE')) { |
30
|
|
|
throw new \LogicException('You must define a CLI_SCHEMA_FILE constant'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
parent::__construct($name); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* All commands take as first argument path to conf file |
39
|
|
|
*/ |
40
|
|
|
protected function configure() |
41
|
|
|
{ |
42
|
|
|
$this->addArgument( |
43
|
|
|
'config-file', |
44
|
|
|
InputArgument::REQUIRED, |
45
|
|
|
'Configuration file path', |
46
|
|
|
null |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
51
|
|
|
{ |
52
|
|
|
parent::execute($input, $output); |
53
|
|
|
|
54
|
|
|
// Elapsed time ? |
55
|
|
|
$this->elapsed(); |
56
|
|
|
|
57
|
|
|
// Config file |
58
|
|
|
$configFile = $this->input->getArgument('config-file'); |
59
|
|
|
$cliFile = $this->getFileFullPath($configFile); |
60
|
|
|
if (!$cliFile) { |
61
|
|
|
throw new \LogicException("Cannot find config file. (Given : [$configFile])"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Load file |
65
|
|
|
$this->config = new Config($configFile, CLI_SCHEMA_FILE); |
66
|
|
|
if (!$this->config->load()) { |
67
|
|
|
$this->output->error('Please fix errors in your configuration file.'); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
protected function getFileFullPath($file) |
73
|
|
|
{ |
74
|
|
|
// Let's try with nothing : full path ? |
75
|
|
|
if (is_file($file)) { |
76
|
|
|
return $file; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Let's try current directory |
80
|
|
|
$cd = getcwd(); |
81
|
|
|
$fullpath = $cd . '/' . $file; |
82
|
|
|
if ($cd && is_file($fullpath)) { |
83
|
|
|
return $fullpath; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Check if it's writable |
87
|
|
|
if (!is_writable($fullpath)) { |
88
|
|
|
throw new \LogicException("Sorry but [$fullpath] is not writable!"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function notify($mail, $commandName = '') |
95
|
|
|
{ |
96
|
|
|
// CommandName |
97
|
|
|
$commandName = !empty($commandName) ? $commandName : $this->getName(); |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
// Cannot notify ? |
101
|
|
|
if (!$this->config->isNotifyOnForCommand('replace')) { |
102
|
|
|
$this->output->info("No notifications sent."); |
|
|
|
|
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Notifications are On |
107
|
|
|
$notificationConfig = $this->config->getNotificationConfig(); |
108
|
|
|
if (empty($notificationConfig)) { |
109
|
|
|
$this->output->warn("Notify is on, but notify configuration are missing."); |
|
|
|
|
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Send mail |
114
|
|
|
$mailHelper = new MailHelper($this->config); |
115
|
|
|
$mailHelper->send($mail, $commandName); |
116
|
|
|
$this->output->info("Notification sent."); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
} |
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: