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

MultiFileSourceBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

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

2 Methods

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