YamlFileLocator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the "elao/api-resources-metadata" package.
5
 *
6
 * Copyright (C) 2016 Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\ApiResourcesMetadata\Resource\Locator;
12
13
use Elao\ApiResourcesMetadata\Exception\InvalidArgumentException;
14
use Symfony\Component\Yaml\Parser;
15
16
class YamlFileLocator implements LocatorInterface
17
{
18
    /** @var string */
19
    private $path;
20
21
    /** @var Parser */
22
    private $parser;
23
24
    public function __construct(string $path)
25
    {
26
        $this->path = $path;
27
        $this->parser = new Parser();
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function locate(): array
34
    {
35
        $parsed = $this->parser->parse(file_get_contents($this->path));
36
        $resources = $parsed['resources'] ?? [];
37
38
        foreach ($resources as $shortName => $resource) {
39
            if (is_array($resource)) {
40
                if (!isset($resource['class'])) {
41
                    throw new InvalidArgumentException(sprintf(
42
                        'Missing class declaration for resource "%s".',
43
                        $shortName
44
                    ));
45
                }
46
47
                $resource = $resource['class'];
48
            }
49
50
            $resources[$shortName] = $resource;
51
        }
52
53
        return $resources;
54
    }
55
}
56