|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Devhelp\PiwikBundle\Command; |
|
5
|
|
|
|
|
6
|
|
|
use Devhelp\Piwik\Api\Method\Method; |
|
7
|
|
|
use Devhelp\PiwikBundle\Command\Param\MethodFinder; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
13
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
|
|
16
|
|
|
class ApiCallCommand extends ContainerAwareCommand |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var ConsoleLogger |
|
21
|
|
|
*/ |
|
22
|
|
|
private $logger; |
|
23
|
|
|
|
|
24
|
|
|
protected function configure() |
|
25
|
|
|
{ |
|
26
|
|
|
// @codingStandardsIgnoreStart |
|
27
|
|
|
$this |
|
28
|
|
|
->setName('devhelp_piwik:api:call') |
|
29
|
|
|
->setDescription('Calls piwik api method') |
|
30
|
|
|
->addArgument( |
|
31
|
|
|
'method', |
|
32
|
|
|
InputArgument::REQUIRED, |
|
33
|
|
|
'Method to call. Can be either a service id (without @) or method name (like "Actions.getPageUrls")' |
|
34
|
|
|
) |
|
35
|
|
|
->addOption( |
|
36
|
|
|
'params', |
|
37
|
|
|
'p', |
|
38
|
|
|
InputOption::VALUE_OPTIONAL, |
|
39
|
|
|
'Method parameters as string (must be valid for parse_str function). '. |
|
40
|
|
|
'They will be merged with params from method service (if it has any) and will override existing ones if names are the same' |
|
41
|
|
|
) |
|
42
|
|
|
->addOption( |
|
43
|
|
|
'api', |
|
44
|
|
|
null, |
|
45
|
|
|
InputOption::VALUE_OPTIONAL, |
|
46
|
|
|
'Api that will be used to call the method (name from configuration). '. |
|
47
|
|
|
'If not specified default api is used. Is ignored if "method" is passed as a service' |
|
48
|
|
|
)->addOption( |
|
49
|
|
|
'show-response', |
|
50
|
|
|
'r', |
|
51
|
|
|
InputOption::VALUE_NONE, |
|
52
|
|
|
'If set then response is logged. '. |
|
53
|
|
|
'Verbosity level must be at least -vv and used together with this option in order to display the response' |
|
54
|
|
|
) |
|
55
|
|
|
->setHelp(<<<HELP |
|
56
|
|
|
The <info>%command.name%</info> command calls Piwik API method. |
|
57
|
|
|
|
|
58
|
|
|
Method can be either service id or method name: |
|
59
|
|
|
|
|
60
|
|
|
<info>php %command.full_name% SitesManager.getAllSites</info> |
|
61
|
|
|
<info>php %command.full_name% my_method_service_id</info> |
|
62
|
|
|
|
|
63
|
|
|
If method name is used then you can use <comment>--api</comment> to set api name from which the method is to be called (if not specified then default api is used): |
|
64
|
|
|
|
|
65
|
|
|
<info>php %command.full_name% SitesManager.getAllSites --api=reader</info> |
|
66
|
|
|
|
|
67
|
|
|
Use <comment>--params</comment> in order to set call parameters (they will be merged with default params defined for the method service): |
|
68
|
|
|
|
|
69
|
|
|
<info>php %command.full_name% my_method_service_id --params='idSite=2&token_auth=MY_TOKEN'</info> |
|
70
|
|
|
|
|
71
|
|
|
Use <comment>-vv</comment> in order to display relevant information about the ongoing request |
|
72
|
|
|
|
|
73
|
|
|
<info>php %command.full_name% my_method_service_id -vv</info> |
|
74
|
|
|
|
|
75
|
|
|
Use <comment>--show-response</comment> together with <comment>-vv</comment> in order to display the response. <comment>Response must be PSR-7 compatible</comment> |
|
76
|
|
|
|
|
77
|
|
|
<info>php %command.full_name% my_method_service_id -vv --show-response</info> |
|
78
|
|
|
|
|
79
|
|
|
HELP |
|
80
|
|
|
); |
|
81
|
|
|
// @codingStandardsIgnoreEnd |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
85
|
|
|
{ |
|
86
|
|
|
$methodArg = $input->getArgument('method'); |
|
87
|
|
|
$apiOption = $input->getOption('api'); |
|
88
|
|
|
|
|
89
|
|
|
$params = []; |
|
90
|
|
|
|
|
91
|
|
|
if ($input->hasOption('params')) { |
|
92
|
|
|
parse_str($input->getOption('params'), $params); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->logger = new ConsoleLogger($output); |
|
96
|
|
|
|
|
97
|
|
|
$method = $this->getMethod($methodArg, $apiOption); |
|
98
|
|
|
|
|
99
|
|
|
$callDetails = []; |
|
100
|
|
|
$callDetails[] = "Calling api..."; |
|
101
|
|
|
$callDetails[] = "- method: ".$method->name(); |
|
102
|
|
|
$callDetails[] = "- url: ".$method->url(); |
|
103
|
|
|
|
|
104
|
|
|
if ($params) { |
|
105
|
|
|
$callDetails[] = "- extra params: "; |
|
106
|
|
|
$callDetails[] = var_export($params, true); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$this->logger->info(implode(PHP_EOL, $callDetails)); |
|
110
|
|
|
|
|
111
|
|
|
$response = $method->call($params); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
$resultDetails = []; |
|
114
|
|
|
$resultDetails[] = "Finished"; |
|
115
|
|
|
|
|
116
|
|
|
if ($input->getOption('show-response') && $response instanceof ResponseInterface) { |
|
117
|
|
|
$resultDetails[] = "Response:"; |
|
118
|
|
|
$resultDetails[] = "- status code: ".$response->getStatusCode(); |
|
119
|
|
|
$resultDetails[] = "- body: ".$response->getBody()->getContents(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$this->logger->info(implode(PHP_EOL, $resultDetails)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $methodArg |
|
127
|
|
|
* @param string $apiOption |
|
128
|
|
|
* @return Method |
|
129
|
|
|
*/ |
|
130
|
|
|
private function getMethod($methodArg, $apiOption) |
|
131
|
|
|
{ |
|
132
|
|
|
$methodFinder = new MethodFinder($this->getContainer(), $this->logger); |
|
133
|
|
|
|
|
134
|
|
|
return $methodFinder->find($methodArg, $apiOption); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.