Completed
Push — clean ( 020cee )
by Akihito
02:31
created

CliRouter::validateArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the BEAR.Package package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package\Provide\Router;
8
9
use Aura\Cli\CliFactory;
10
use Aura\Cli\Context\OptionFactory;
11
use Aura\Cli\Status;
12
use Aura\Cli\Stdio;
13
use BEAR\Package\Annotation\StdIn;
14
use BEAR\Sunday\Extension\Router\RouterInterface;
15
use Ray\Di\Di\Inject;
16
use Ray\Di\Di\Named;
17
18
class CliRouter implements RouterInterface
19
{
20
    /**
21
     * @var RouterInterface
22
     */
23
    private $router;
24
25
    /**
26
     * @var \LogicException
27
     */
28
    private $exception;
29
30
    /**
31
     * @var Stdio
32
     */
33
    private $stdIo;
34
35
    /**
36
     * @var string
37
     */
38
    private $stdIn;
39
40
    /**
41
     * @param string $stdIn
42
     *
43
     * @Inject
44
     * @StdIn
45
     */
46 10
    public function setStdIn($stdIn)
47
    {
48 10
        $this->stdIn = $stdIn;
49 10
    }
50
51
    /**
52
     * @param RouterInterface $router
53
     * @param \LogicException $exception
54
     * @param Stdio           $stdIo
55
     *
56
     * @Inject
57
     * @Named("original")
58
     */
59 10
    public function __construct(RouterInterface $router, \LogicException $exception = null, Stdio $stdIo = null)
60
    {
61 10
        $this->router = $router;
62 10
        $this->exception = $exception;
63 10
        $this->stdIo = $stdIo ?: (new CliFactory)->newStdio();
64 10
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 7
    public function match(array $globals, array $server)
70
    {
71 7
        $this->validateArgs($globals);
72 6
        list($method, $query, $server) = $this->parseGlobals($globals);
73 6
        $this->setQuery($method, $query, $globals, $server);
74
75 6
        return $this->router->match($globals, $server);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 1
    public function generate($name, $data)
82
    {
83 1
        return $this->router->generate($name, $data);
84
    }
85
86
    /**
87
     * Set user input query to $globals or &$server
88
     *
89
     * @param string $method
90
     * @param array  $query
91
     * @param array  $globals
92
     * @param array  $server
93
     */
94 6
    private function setQuery($method, array $query, array &$globals, array &$server)
95
    {
96 6
        if ($method === 'get') {
97 1
            $globals['_GET'] = $query;
98
99 1
            return;
100
        }
101 5
        if ($method === 'post') {
102 1
            $globals['_POST'] = $query;
103
104 1
            return;
105
        }
106 4
        $server = $this->getStdIn($method, $query, $server);
107 4
    }
108
109
    /**
110
     * @param string $command
111
     */
112 1
    private function error($command)
113
    {
114 1
        $help = new CliRouterHelp(new OptionFactory);
115 1
        $this->stdIo->outln($help->getHelp($command));
116 1
    }
117
118
    /**
119
     * @param int $status
120
     *
121
     * @SuppressWarnings(PHPMD)
122
     */
123 1
    private function exitProgram($status)
124
    {
125 1
        if ($this->exception) {
126 1
            throw $this->exception;
127
        }
128
        // @codeCoverageIgnoreStart
129
        exit($status);
130
        // @codeCoverageIgnoreEnd
131
    }
132
133
    /**
134
     * Return StdIn in PUT, PATCH or DELETE
135
     *
136
     * @param string $method
137
     * @param array  $query
138
     * @param array  $server
139
     *
140
     * @return array
141
     */
142 4
    private function getStdIn($method, array $query, array &$server)
143
    {
144 4
        if ($method === 'put' || $method === 'patch' || $method === 'delete') {
145 3
            $server[HttpMethodParams::CONTENT_TYPE] = HttpMethodParams::FORM_URL_ENCODE;
146 3
            file_put_contents($this->stdIn, http_build_query($query));
147
148 3
            return $server;
149
        }
150
151 1
        return $server;
152
    }
153
154
    /**
155
     * Validate input
156
     *
157
     * @param array $globals
158
     */
159 7
    private function validateArgs(array $globals)
160
    {
161 7
        if ($globals['argc'] !== 3) {
162 1
            $this->error(basename($globals['argv'][0]));
163 1
            $this->exitProgram(Status::USAGE);
164
        };
165 6
    }
166
167
    /**
168
     * Return $method, $query, $server from $globals
169
     *
170
     * @param array $globals
171
     *
172
     * @return array
173
     */
174 6
    private function parseGlobals(array $globals)
175
    {
176 6
        list(, $method, $uri) = $globals['argv'];
177 6
        $parsedUrl = parse_url($uri);
178 6
        $query = [];
179 6
        if (isset($parsedUrl['query'])) {
180 5
            parse_str($parsedUrl['query'], $query);
181
        }
182
        $server = [
183 6
            'REQUEST_METHOD' => strtoupper($method),
184 6
            'REQUEST_URI' => $parsedUrl['path']
185
        ];
186
187 6
        return [$method, $query, $server];
188
    }
189
}
190