Passed
Push — master ( 3c9073...c9c108 )
by Pieter
04:28
created

supportsNormalization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace W2w\Laravel\Apie\Plugins\IlluminateTranslation\Normalizers;
5
6
use Illuminate\Contracts\Translation\Translator;
7
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
8
use Throwable;
9
use W2w\Lib\Apie\Plugins\Core\Normalizers\ExceptionNormalizer;
10
use W2w\Lib\ApieObjectAccessNormalizer\Exceptions\LocalizationableException;
11
12
class LocationableExceptionNormalizer implements NormalizerInterface
13
{
14
    /**
15
     * @var ExceptionNormalizer
16
     */
17
    private $exceptionNormalizer;
18
19
    /**
20
     * @var Translator
21
     */
22
    private $translator;
23
24
    public function __construct(ExceptionNormalizer $exceptionNormalizer, Translator $translator)
25
    {
26
        $this->exceptionNormalizer = $exceptionNormalizer;
27
        $this->translator = $translator;
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function normalize($object, string $format = null, array $context = [])
34
    {
35
        /** @var LocalizationableException|Throwable $object */
36
        $data = $this->exceptionNormalizer->normalize($object, $format, $context);
37
        $i18n = $object->getI18n();
0 ignored issues
show
Bug introduced by
The method getI18n() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as W2w\Lib\ApieObjectAccess...ns\ObjectWriteException or W2w\Lib\ApieObjectAccess...s\NameNotFoundException or W2w\Lib\ApieObjectAccess...s\ObjectAccessException or W2w\Lib\ApieObjectAccess...ons\ValidationException or W2w\Lib\ApieObjectAccess...ouldNotConvertException or W2w\Lib\Apie\Exceptions\...ForValueObjectException or W2w\Lib\Apie\Exceptions\...dNotBeNegativeException or W2w\Lib\Apie\Exceptions\InvalidPageLimitException or W2w\Lib\Apie\Exceptions\InvalidIdException or W2w\Lib\Apie\Exceptions\MethodNotAllowedException or W2w\Lib\ApieObjectAccess...ns\ObjectWriteException or W2w\Lib\ApieObjectAccess...s\NameNotFoundException or W2w\Lib\ApieObjectAccess...s\ObjectAccessException or W2w\Lib\ApieObjectAccess...ons\ValidationException or W2w\Lib\ApieObjectAccess...ouldNotConvertException or W2w\Lib\Apie\Exceptions\...ForValueObjectException or W2w\Lib\Apie\Exceptions\...dNotBeNegativeException or W2w\Lib\Apie\Exceptions\InvalidPageLimitException or W2w\Lib\Apie\Exceptions\InvalidIdException or W2w\Lib\Apie\Exceptions\MethodNotAllowedException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        /** @scrutinizer ignore-call */ 
38
        $i18n = $object->getI18n();
Loading history...
38
        $replacements = [];
39
        foreach ($i18n->getReplacements() as $key => $value) {
40
            if (is_array($value)) {
41
                $replacements[$key] = json_encode($value);
42
            } else {
43
                $replacements[$key] = $value;
44
            }
45
        }
46
        $data['message'] = $this->translator->choice(
47
            'apie::' . $i18n->getMessageString(),
48
            $i18n->getAmount(),
49
            $replacements
50
        );
51
        return $data;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function supportsNormalization($data, string $format = null)
58
    {
59
        return $this->exceptionNormalizer->supportsNormalization($data, $format) && $data instanceof LocalizationableException;
60
    }
61
}
62