Passed
Pull Request — master (#267)
by Sam
02:06
created

DirectorySourceBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Language;
4
5
use Digia\GraphQL\Error\InvariantException;
6
7
/**
8
 * Class DirectorySourceBuilder
9
 * @package Digia\GraphQL\Language
10
 */
11
class DirectorySourceBuilder implements SourceBuilderInterface
12
{
13
14
    /**
15
     * @var string
16
     */
17
    private $directoryPath;
18
19
    /**
20
     * DirectorySourceBuilder constructor.
21
     * @param string $directoryPath
22
     */
23
    public function __construct(string $directoryPath)
24
    {
25
        $this->directoryPath = $directoryPath;
26
    }
27
28
29
    /**
30
     * @inheritdoc
31
     *
32
     * @throws InvariantException
33
     */
34
    public function buildSource(): Source
35
    {
36
        $iterator       = new \DirectoryIterator($this->directoryPath);
37
        $combinedSource = '';
38
39
        foreach ($iterator as $item) {
40
            // Skip directories and dot files
41
            if ($item->isDot() || !$item->isFile()) {
42
                continue;
43
            }
44
45
            $itemPath = $item->getPathname();
46
47
            if (!$item->isReadable()) {
48
                throw new InvariantException(sprintf('The file %s is not readable', $itemPath));
49
            }
50
51
            $combinedSource .= \file_get_contents($itemPath);
52
        }
53
54
        return new Source($combinedSource);
55
    }
56
}
57