Completed
Push — master ( 0d17ec...34df22 )
by Yaro
02:05
created

Blueprint::getDisk()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php 
2
3
namespace Yaro\ApiDocs;
4
5
use Illuminate\Contracts\Filesystem\Factory;
6
use Illuminate\Filesystem\FilesystemAdapter;
7
use Illuminate\Contracts\Config\Repository as Config;
8
use Yaro\ApiDocs\Exceptions\InvalidFilesystemDisc;
9
10
class Blueprint
11
{
12
    private $filesystem;
13
    private $config;
14
    private $endpoints = [];
15
16
    public function __construct(Factory $filesystem, Config $config)
17
    {
18
        $this->filesystem = $filesystem;
19
        $this->config = $config;
20
    }
21
    
22
    public function setEndpoints(array $rawEndpoints)
23
    {
24
        $endpoints = [];
25
        $delimiter = $this->config->get('yaro.apidocs.blueprint.reference_delimiter');
26
        
27
        foreach ($rawEndpoints as $group => $groupEndpoints) {
28
            $group = preg_replace('~\.~', $delimiter, $group);
29
            $endpoints[$group] = $groupEndpoints;
30
        }
31
        
32
        $this->endpoints = $endpoints;
33
    }
34
    
35
    public function render()
36
    {
37
        $host = $this->config->get('yaro.apidocs.blueprint.host');
38
        if (!$host) {
39
            $prefix = $this->config->get('yaro.apidocs.prefix', 'api');
40
            $host = url($prefix);
41
        }
42
        
43
        return view('apidocs::blueprint', [
44
            'endpoints'    => $this->endpoints, 
45
            'host'         => $host, 
46
            'title'        => $this->config->get('yaro.apidocs.blueprint.title'), 
47
            'introduction' => $this->config->get('yaro.apidocs.blueprint.introduction')
48
        ])->render();
49
    }
50
51
    public function create(string $snapshotName = '', string $diskName = '')
52
    {
53
        $snapshotName = $snapshotName ?: 'blueprint_'. date('Y-m-d_H-i-s');
54
        $diskName = $diskName ?: $this->config->get('yaro.apidocs.blueprint.disc');
55
        
56
        $disk = $this->getDisk($diskName);
57
58
        $fileName = $snapshotName.'.apib';
59
        $fileName = pathinfo($fileName, PATHINFO_BASENAME);
60
61
        $this->createFile($fileName, $disk);
1 ignored issue
show
Documentation introduced by
$disk is of type object<Illuminate\Contra...\Filesystem\Filesystem>, but the function expects a object<Illuminate\Filesystem\FilesystemAdapter>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
        
63
        return $snapshotName;
64
    }
65
66
    private function getDisk(string $diskName)
67
    {
68
        $path = sprintf('filesystems.disks.%s', $diskName);
69
        if (is_null($this->config->get($path))) {
70
            throw new InvalidFilesystemDisc($diskName);
71
        }
72
73
        return $this->filesystem->disk($diskName);
74
    }
75
76
    private function createFile(string $fileName, FilesystemAdapter $disk)
77
    {
78
        $tempFileHandle = tmpfile();
79
        fwrite($tempFileHandle, $this->render());
80
81
        $disk->put($fileName, $tempFileHandle);
82
83
        fclose($tempFileHandle);
84
    }
85
    
86
}
87