Completed
Push — master ( 90ccc1...22512f )
by Kamil
35:43
created

MetadataProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 66
rs 10
c 5
b 1
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B findMetadataBySubject() 0 30 5
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Metadata\Provider;
13
14
use Doctrine\Common\Persistence\ObjectRepository;
15
use Sylius\Component\Metadata\Compiler\MetadataCompilerInterface;
16
use Sylius\Component\Metadata\Model\MetadataSubjectInterface;
17
use Sylius\Component\Metadata\HierarchyProvider\MetadataHierarchyProviderInterface;
18
use Sylius\Component\Metadata\Model\MetadataContainerInterface;
19
20
/**
21
 * @author Kamil Kokot <[email protected]>
22
 */
23
final class MetadataProvider implements MetadataProviderInterface
24
{
25
    /**
26
     * @var ObjectRepository
27
     */
28
    private $metadataContainerRepository;
29
30
    /**
31
     * @var MetadataCompilerInterface
32
     */
33
    private $metadataCompiler;
34
35
    /**
36
     * @var MetadataHierarchyProviderInterface
37
     */
38
    private $metadataHierarchyProvider;
39
40
    /**
41
     * @param ObjectRepository $metadataContainerRepository
42
     * @param MetadataCompilerInterface $metadataCompiler
43
     * @param MetadataHierarchyProviderInterface $metadataHierarchyProvider
44
     */
45
    public function __construct(
46
        ObjectRepository $metadataContainerRepository,
47
        MetadataCompilerInterface $metadataCompiler,
48
        MetadataHierarchyProviderInterface $metadataHierarchyProvider
49
    ) {
50
        $this->metadataContainerRepository = $metadataContainerRepository;
51
        $this->metadataCompiler = $metadataCompiler;
52
        $this->metadataHierarchyProvider = $metadataHierarchyProvider;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function findMetadataBySubject(MetadataSubjectInterface $metadataSubject)
59
    {
60
        $identifiers = $this->metadataHierarchyProvider->getHierarchyByMetadataSubject($metadataSubject);
61
62
        $parents = [];
63
        $baseMetadata = null;
64
        foreach ($identifiers as $identifier) {
65
            /** @var MetadataContainerInterface $metadataContainer */
66
            // TODO: Use find($identifier) after Resource refactoring (PR #2255)
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
            $metadataContainer = $this->metadataContainerRepository->findOneBy(['id' => $identifier]);
68
69
            if (null === $metadataContainer) {
70
                continue;
71
            }
72
73
            if (null === $baseMetadata) {
74
                $baseMetadata = $metadataContainer->getMetadata();
75
76
                continue;
77
            }
78
79
            $parents[] = $metadataContainer->getMetadata();
80
        }
81
82
        if (null === $baseMetadata) {
83
            return null;
84
        }
85
86
        return $this->metadataCompiler->compile($baseMetadata, $parents);
87
    }
88
}
89