1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* |
8
|
|
|
* @license GPL-3.0 |
9
|
|
|
* |
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Addiks\SymfonyGenerics\Arguments\ArgumentFactory; |
14
|
|
|
|
15
|
|
|
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory; |
16
|
|
|
use Addiks\SymfonyGenerics\Arguments\Argument; |
17
|
|
|
use Addiks\SymfonyGenerics\Arguments\RequestArgument; |
18
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
19
|
|
|
use Webmozart\Assert\Assert; |
20
|
|
|
|
21
|
|
View Code Duplication |
final class RequestArgumentFactory implements ArgumentFactory |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var RequestStack |
26
|
|
|
*/ |
27
|
|
|
private $requestStack; |
28
|
|
|
|
29
|
25 |
|
public function __construct(RequestStack $requestStack) |
30
|
|
|
{ |
31
|
25 |
|
$this->requestStack = $requestStack; |
32
|
25 |
|
} |
33
|
|
|
|
34
|
15 |
|
public function understandsString(string $source): bool |
35
|
|
|
{ |
36
|
15 |
|
return 1 === preg_match('/^\$[a-zA-Z0-9_]+$/is', $source); |
37
|
|
|
} |
38
|
|
|
|
39
|
7 |
|
public function understandsArray(array $source): bool |
40
|
|
|
{ |
41
|
7 |
|
return isset($source['key']) && isset($source['type']) && $source['type'] === 'request'; |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
public function createArgumentFromString(string $source): Argument |
45
|
|
|
{ |
46
|
5 |
|
Assert::true($this->understandsString($source)); |
47
|
|
|
|
48
|
|
|
/** @var string $key */ |
49
|
2 |
|
$key = substr($source, 1); |
50
|
|
|
|
51
|
2 |
|
return new RequestArgument($this->requestStack, $key); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
public function createArgumentFromArray(array $source): Argument |
55
|
|
|
{ |
56
|
3 |
|
Assert::keyExists($source, 'key'); |
57
|
|
|
|
58
|
2 |
|
return new RequestArgument($this->requestStack, $source['key']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.