Passed
Pull Request — master (#20)
by Mathias
01:42
created

HashidsParamConverter::getHash()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 2
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
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
        $this->decodeArgumentsController($request, $configuration);
30
31
        return $this->continueWithNextParamConverters();
32
    }
33
34
    public function supports(ParamConverter $configuration): bool
35
    {
36
        return true;
37
    }
38
39
    private function decodeArgumentsController(Request $request, ParamConverter $configuration): void
40
    {
41
        $hash = $this->getHash($request, $configuration);
42
43
        if ($this->isSkippable($hash)) {
44
            return;
45
        }
46
47
        $name = $configuration->getName();
48
        $hashids = $this->hashids->decode($hash);
49
50
        if ($this->hasHashidDecoded($hashids)) {
51
            $request->attributes->set($name, current($hashids));
52
        }
53
54
        if (!$this->autoConvert && !$this->hasHashidDecoded($hashids)) {
55
            throw new \LogicException(sprintf('Unable to decode parameter "%s".', $name));
56
        }
57
    }
58
59
    private function getHash(Request $request, ParamConverter $configuration): ?string
60
    {
61
        $name = $configuration->getName();
62
63
        if (empty($name)) {
64
            return null;
65
        }
66
67
        $hash = $request->attributes->get('_hash_' . $name);
68
69
        if (!isset($hash)) {
70
            $hash = $this->getHashFromAliases($hash, $request, $configuration);
71
        }
72
73
        if (!isset($hash) && $this->autoConvert) {
74
            $hash = (string) $request->attributes->get($name);
75
        }
76
77
        return $hash;
78
    }
79
80
    private function getHashFromAliases(?string $hash, Request $request, ParamConverter $configuration): ?string
81
    {
82
        if (!$request->attributes->has('hashids_prevent_alias')) {
83
            foreach (['hashid', 'id'] as $alias) {
84
                if ($request->attributes->has($alias)) {
85
                    $hash = $request->attributes->get($alias);
86
                    $request->attributes->set('hashids_prevent_alias', true);
87
                    $configuration->setName('id');
88
                    break;
89
                }
90
            }
91
        }
92
93
        return $hash;
94
    }
95
96
    private function isSkippable($hash): bool
97
    {
98
        return empty($hash) || !$this->allCharsAreInAlphabet($hash);
99
    }
100
101
    private function allCharsAreInAlphabet($hash): bool
102
    {
103
        return (bool) preg_match(sprintf('{^[%s]+$}', $this->alphabet), $hash);
104
    }
105
106
    private function hasHashidDecoded($hashids): bool
107
    {
108
        return $hashids && is_iterable($hashids) && is_int(reset($hashids));
109
    }
110
111
    private function continueWithNextParamConverters(): bool
112
    {
113
        return !$this->passthrough;
114
    }
115
}
116