GridMetadataFactory::resolveGridMetadata()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 6
Ratio 28.57 %

Importance

Changes 0
Metric Value
dl 6
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
use Metadata\MetadataFactory;
8
use Psi\Component\Grid\Metadata\GridMetadata;
9
10
class GridMetadataFactory implements GridMetadataFactoryInterface
11
{
12
    private $metadataFactory;
13
14
    public function __construct(MetadataFactory $metadataFactory)
15
    {
16
        $this->metadataFactory = $metadataFactory;
17
    }
18
19
    public function getGridMetadata(string $classFqn, string $variant): GridMetadata
20
    {
21
        if (null === $metadata = $this->metadataFactory->getMetadataForClass($classFqn)) {
22
            throw new \InvalidArgumentException('Could not locate grid metadata');
23
        }
24
25
        return $this->resolveGridMetadata($metadata->getGrids(), $variant);
26
    }
27
28
    private function resolveGridMetadata(array $grids, string $variant = null)
29
    {
30
        if (empty($grids)) {
31
            throw new \InvalidArgumentException('No grid variants are available');
32
        }
33
34
        // if no explicit grid variant is requested, return the first one that
35
        // was defined.
36
        if (null === $variant) {
37
            return reset($grids);
38
        }
39
40 View Code Duplication
        if (!isset($grids[$variant])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
            throw new \InvalidArgumentException(sprintf(
42
                'Unknown grid variant "%s", available variants: "%s"',
43
                $variant, implode('", "', array_keys($grids))
44
            ));
45
        }
46
47
        return $grids[$variant];
48
    }
49
}
50