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

MultiFileSourceBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A build() 0 13 4
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