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

FileSourceBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A build() 0 7 3
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 FileSourceBuilder
10
 * @package Digia\GraphQL\Language
11
 */
12
class FileSourceBuilder implements SourceBuilderInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $filePath;
18
19
    /**
20
     * FileSourceBuilder constructor.
21
     * @param string $filePath
22
     */
23
    public function __construct(string $filePath)
24
    {
25
        $this->filePath = $filePath;
26
    }
27
28
    /**
29
     * @inheritdoc
30
     *
31
     * @throws FileNotFoundException
32
     * @throws InvariantException
33
     */
34
    public function build(): Source
35
    {
36
        if (!\file_exists($this->filePath) || !\is_readable($this->filePath)) {
37
            throw new FileNotFoundException(sprintf('The file %s cannot be found or is not readable', $this->filePath));
38
        }
39
40
        return new Source(\file_get_contents($this->filePath));
41
    }
42
}
43