• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sulu / SuluContentBundle / 11978668947

22 Nov 2024 07:11PM UTC coverage: 97.052% (-2.9%) from 100.0%
11978668947

Pull #271

github

web-flow
Merge 85d51e611 into a23a5b050
Pull Request #271: Draft: Base implementation of website content resolving

268 of 353 new or added lines in 17 files covered. (75.92%)

2798 of 2883 relevant lines covered (97.05%)

10.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.1
/Content/Application/PropertyResolver/Resolver/BlockPropertyResolver.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Sulu.
7
 *
8
 * (c) Sulu GmbH
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13

14
namespace Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\Resolver;
15

16
use Psr\Log\LoggerInterface;
17
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FieldMetadata;
18
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata;
19
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
20
use Sulu\Bundle\ContentBundle\Content\Application\MetadataResolver\MetadataResolver;
21

22
class BlockPropertyResolver implements PropertyResolverInterface
23
{
24
    private MetadataResolver $metadataResolver;
25

26
    public function __construct(
27
        private readonly LoggerInterface $logger,
28
        private readonly bool $debug = false,
29
    ) {
30
    }
12✔
31

32
    /**
33
     * Prevent circular dependency by injecting the MetadataResolver after instantiation.
34
     */
35
    public function setMetadataResolver(MetadataResolver $metadataResolver): void
36
    {
37
        $this->metadataResolver = $metadataResolver;
11✔
38
    }
39

40
    /**
41
     * @param array<array<mixed>>|mixed $data
42
     */
43
    public function resolve(mixed $data, string $locale, array $params = []): ContentView
44
    {
45
        $metadata = $params['metadata'] ?? null;
10✔
46
        $returnedParams = $params;
10✔
47
        unset($returnedParams['metadata']); // TODO we may should implement a PropertyResolverAwareMetadataInterface
10✔
48

49
        if (!\is_array($data) || !\array_is_list($data)) {
10✔
50
            return ContentView::create([], [...$returnedParams]);
8✔
51
        }
52

53
        \assert($metadata instanceof FieldMetadata, 'Metadata must be set to resolve blocks.');
54
        $metadataTypes = $metadata->getTypes();
4✔
55

56
        $contentViews = [];
4✔
57
        foreach ($data as $key => $block) {
4✔
58
            if (!\is_array($block) || !isset($block['type']) || !\is_string($block['type'])) {
1✔
NEW
59
                continue;
×
60
            }
61

62
            $type = $block['type'];
1✔
63
            $formMetadata = $metadataTypes[$type] ?? null;
1✔
64

65
            if (!$formMetadata instanceof FormMetadata) {
1✔
NEW
66
                $errorMessage = \sprintf(
×
NEW
67
                    'Metadata type "%s" in "%s" not found, founded types are: "%s"',
×
NEW
68
                    $type,
×
NEW
69
                    $metadata->getName(),
×
NEW
70
                    \implode('", "', \array_keys($metadataTypes)),
×
NEW
71
                );
×
72

NEW
73
                $this->logger->error($errorMessage);
×
74

NEW
75
                if ($this->debug) {
×
NEW
76
                    throw new \UnexpectedValueException($errorMessage);
×
77
                }
78

NEW
79
                $type = $metadata->getDefaultType();
×
NEW
80
                $formMetadata = $metadataTypes[$type] ?? null;
×
NEW
81
                if (!$formMetadata instanceof FormMetadata) {
×
NEW
82
                    continue;
×
83
                }
84
            }
85

86
            $contentViews[$key] = ContentView::create(
1✔
87
                \array_merge(
1✔
88
                    ['type' => $type],
1✔
89
                    $this->metadataResolver->resolveItems($formMetadata->getItems(), $block, $locale)
1✔
90
                ),
1✔
91
                [
1✔
92
                    ...$returnedParams,
1✔
93
                ]
1✔
94
            );
1✔
95
        }
96

97
        return ContentView::create($contentViews, [...$returnedParams]);
4✔
98
    }
99

100
    public static function getType(): string
101
    {
102
        return 'block';
1✔
103
    }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc