Completed
Push — master ( c34cac...3c68a7 )
by Pieter
04:47
created

ApiResourcesFromNamespace   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 40
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getApiResources() 0 3 1
A createApiResources() 0 13 3
A __construct() 0 8 2
1
<?php
2
namespace W2w\Lib\Apie\Core\Resources;
3
4
use HaydenPierce\ClassFinder\ClassFinder;
5
use W2w\Lib\Apie\Exceptions\BadConfigurationException;
6
use W2w\Lib\Apie\Plugins\ApplicationInfo\ApiResources\ApplicationInfo;
7
use W2w\Lib\Apie\Plugins\StatusCheck\ApiResources\Status;
8
9
/**
10
 * Returns all classes in a specific namespace. You require to install haydenpierce/class-finder with composer to get
11
 * this working.
12
 */
13
class ApiResourcesFromNamespace implements ApiResourcesInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $namespace;
19
20
    public function __construct(string $namespace)
21
    {
22
        // @codeCoverageIgnoreStart
23
        if (!class_exists(ClassFinder::class)) {
24
            throw new BadConfigurationException(__CLASS__ . ' can only be used if you require haydenpierce/class-finder in your project.');
25
        }
26
        // @codeCoverageIgnoreEnd
27
        $this->namespace = $namespace;
28
    }
29
30
    /**
31
     * Returns all api resources.
32
     *
33
     * @return string[]
34
     */
35
    public function getApiResources(): array
36
    {
37
        return ClassFinder::getClassesInNamespace($this->namespace);
38
    }
39
40
    public static function createApiResources(string $namespace, bool $defaultResources = true): array
41
    {
42
        // @codeCoverageIgnoreStart
43
        if (!class_exists(ClassFinder::class)) {
44
            throw new BadConfigurationException(__CLASS__ . ' can only be used if you require haydenpierce/class-finder in your project.');
45
        }
46
        // @codeCoverageIgnoreEnd
47
        $classes = ClassFinder::getClassesInNamespace($namespace);
48
        if ($defaultResources) {
49
            $classes[] = ApplicationInfo::class;
50
            $classes[] = Status::class;
51
        }
52
        return $classes;
53
    }
54
}
55