PlaceholderParser   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A decodePlaceholderData() 0 5 1
A __construct() 0 6 1
A htmlEntityDecodePlaceholderData() 0 3 1
A parse() 0 31 4
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Yves\Coremedia\ApiResponse\Parser;
9
10
use Generated\Shared\Transfer\CoremediaPlaceholderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ediaPlaceholderTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SprykerEco\Shared\Coremedia\CoremediaConfig as SharedCoremediaConfig;
12
use SprykerEco\Yves\Coremedia\CoremediaConfig;
13
use SprykerEco\Yves\Coremedia\Dependency\Service\CoremediaToUtilEncodingServiceInterface;
14
15
class PlaceholderParser implements PlaceholderParserInterface
16
{
17
    /**
18
     * @var \SprykerEco\Yves\Coremedia\Dependency\Service\CoremediaToUtilEncodingServiceInterface
19
     */
20
    protected $utilEncodingService;
21
22
    /**
23
     * @var \SprykerEco\Yves\Coremedia\CoremediaConfig
24
     */
25
    protected $config;
26
27
    /**
28
     * @param \SprykerEco\Yves\Coremedia\Dependency\Service\CoremediaToUtilEncodingServiceInterface $utilEncodingService
29
     * @param \SprykerEco\Yves\Coremedia\CoremediaConfig $config
30
     */
31
    public function __construct(
32
        CoremediaToUtilEncodingServiceInterface $utilEncodingService,
33
        CoremediaConfig $config
34
    ) {
35
        $this->utilEncodingService = $utilEncodingService;
36
        $this->config = $config;
37
    }
38
39
    /**
40
     * @param string $content
41
     *
42
     * @return \Generated\Shared\Transfer\CoremediaPlaceholderTransfer[]
43
     */
44
    public function parse(string $content): array
45
    {
46
        preg_match_all(
47
            $this->config->getPlaceholderPattern(),
48
            $content,
49
            $matches
50
        );
51
52
        $placeholders = [];
53
54
        if (!$matches[SharedCoremediaConfig::PREG_MATCH_PLACEHOLDER_KEY]) {
55
            return [];
56
        }
57
58
        $placeholdersData = array_unique($matches[SharedCoremediaConfig::PREG_MATCH_PLACEHOLDER_KEY]);
59
60
        foreach ($placeholdersData as $placeholderKey => $placeholderData) {
61
            $decodedPlaceholderData = $this->decodePlaceholderData($placeholderData);
62
63
            if (!$decodedPlaceholderData) {
64
                continue;
65
            }
66
67
            $coreMediaPlaceholderTransfer = (new CoremediaPlaceholderTransfer())
68
                ->fromArray($decodedPlaceholderData, true)
69
                ->setPlaceholderBody($matches[0][$placeholderKey]);
70
71
            $placeholders[] = $coreMediaPlaceholderTransfer;
72
        }
73
74
        return $placeholders;
75
    }
76
77
    /**
78
     * @param string $placeholderData
79
     *
80
     * @return array|null
81
     */
82
    protected function decodePlaceholderData(string $placeholderData): ?array
83
    {
84
        return $this->utilEncodingService->decodeJson(
85
            $this->htmlEntityDecodePlaceholderData($placeholderData),
86
            true
87
        );
88
    }
89
90
    /**
91
     * @param string $placeholderData
92
     *
93
     * @return string
94
     */
95
    protected function htmlEntityDecodePlaceholderData(string $placeholderData): string
96
    {
97
        return html_entity_decode($placeholderData, ENT_QUOTES, 'UTF-8');
98
    }
99
}
100