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

MultiFileSourceBuilder::__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 MultiFileSourceBuilder
9
 * @package Digia\GraphQL\Language
10
 */
11
class MultiFileSourceBuilder implements SourceBuilderInterface
12
{
13
14
    /**
15
     * @var string[]
16
     */
17
    private $filePaths;
18
19
    /**
20
     * MultiFileSourceBuilder constructor.
21
     * @param string[] $directoryPaths
22
     */
23
    public function __construct(array $directoryPaths)
24
    {
25
        $this->filePaths = $directoryPaths;
26
    }
27
28
    /**
29
     * @inheritdoc
30
     *
31
     * @throws InvariantException
32
     */
33
    public function buildSource(): Source
34
    {
35
        $combinedSource = '';
36
37
        foreach ($this->filePaths as $filePath) {
38
            if (!\file_exists($filePath) || !\is_readable($filePath)) {
39
                throw new InvariantException(sprintf('The file %s cannot be found or is not readable', $filePath));
40
            }
41
42
            $combinedSource .= \file_get_contents($filePath);
43
        }
44
45
        return new Source($combinedSource);
46
    }
47
}
48