Passed
Pull Request — master (#20)
by Mathias
02:24
created

HashidsParamConverter::isSkippable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roukmoute\HashidsBundle\ParamConverter;
6
7
use Hashids\HashidsInterface;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
9
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
12
class HashidsParamConverter implements ParamConverterInterface
13
{
14
    private string $alphabet;
15
    private bool $autoConvert;
16
    private HashidsInterface $hashids;
17
    private bool $passthrough;
18
19
    public function __construct(HashidsInterface $hashids, bool $passthrough, bool $autoConvert, string $alphabet)
20
    {
21
        $this->hashids = $hashids;
22
        $this->passthrough = $passthrough;
23
        $this->autoConvert = $autoConvert;
24
        $this->alphabet = $alphabet;
25
    }
26
27
    public function apply(Request $request, ParamConverter $configuration): bool
28
    {
29
        $decodedSuccessfuly = $this->decodeHashidOnRoute($request);
30
31
        if ($decodedSuccessfuly === false) {
32
            $this->decodeArgumentsController($request, $configuration);
33
        }
34
35
        return $this->continueWithNextParamConverters();
36
    }
37
38
    public function supports(ParamConverter $configuration): bool
39
    {
40
        return true;
41
    }
42
43
    private function decodeHashidOnRoute(Request $request): bool
44
    {
45
        foreach (['hashid', 'id'] as $item) {
46
            $hashids = $this->hashids->decode($request->attributes->get($item));
47
            if ($this->hasHashidDecoded($hashids)) {
48
                $hashid = current($hashids);
49
50
                $request->attributes->remove($item);
51
                $request->attributes->set('id', $hashid);
52
53
                return true;
54
            }
55
        }
56
57
        return false;
58
    }
59
60
    private function decodeArgumentsController(Request $request, ParamConverter $configuration): void
61
    {
62
        $name = $configuration->getName();
63
64
        $attributes = array_map(function ($name) {
65
            return str_replace('_hash_', '', $name);
66
        }, $request->attributes->keys());
67
68
        $hash = null;
69
70
        if (in_array($name, $attributes, true)) {
71
            $hash = $request->attributes->get('_hash_' . $name);
72
        }
73
74
        if (!isset($hash) && $this->autoConvert) {
75
            $hash = (string) $request->attributes->get($name);
76
        }
77
78
        if ($this->isSkippable($hash)) {
79
            return;
80
        }
81
82
        $hashids = $this->hashids->decode($hash);
83
84
        if ($this->hasHashidDecoded($hashids)) {
85
            $request->attributes->set($name, current($hashids));
86
        }
87
88
        if (!$this->autoConvert && !$this->hasHashidDecoded($hashids)) {
89
            throw new \LogicException(sprintf('Unable to decode parameter "%s".', $name));
90
        }
91
    }
92
93
    private function isSkippable($hash): bool
94
    {
95
        return empty($hash) || !$this->allCharsAreInAlphabet($hash);
96
    }
97
98
    private function allCharsAreInAlphabet($hash): bool
99
    {
100
        return (bool) preg_match(sprintf('{^[%s]+$}', $this->alphabet), $hash);
101
    }
102
103
    private function hasHashidDecoded($hashids): bool
104
    {
105
        return $hashids && is_iterable($hashids) && is_int(reset($hashids));
106
    }
107
108
    private function continueWithNextParamConverters(): bool
109
    {
110
        return !$this->passthrough;
111
    }
112
}
113