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