Passed
Pull Request — master (#20)
by Mathias
01:32
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
        $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
        $name = $configuration->getName();
42
        $hash = $this->getHash($request, $name);
43
44
        if ($this->isSkippable($hash)) {
45
            return;
46
        }
47
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, string &$name): ?string
60
    {
61
        if (empty($name)) {
62
            return null;
63
        }
64
65
        $attributes = array_map(function ($name) {
66
            return str_replace('_hash_', '', $name);
67
        }, $request->attributes->keys());
68
69
        $hash = null;
70
71
        if (in_array($name, $attributes, true)) {
72
            $hash = $request->attributes->get('_hash_' . $name);
73
        }
74
75
        if (!isset($hash) && $this->autoConvert) {
76
            $hash = (string) $request->attributes->get($name);
77
        }
78
79
        if (!isset($hash) && !$request->attributes->has('hashids_prevent_alias')) {
80
            foreach (['hashid', 'id'] as $alias) {
81
                if ($request->attributes->has($alias)) {
82
                    $hash = $request->attributes->get($alias);
83
                    $request->attributes->set('hashids_prevent_alias', true);
84
                    $name = 'id';
85
                    break;
86
                }
87
            }
88
        }
89
90
        return $hash;
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