Test Failed
Push — feature-laravel-5.4 ( fbc913...c5d92a )
by Kirill
03:14
created

GitHubDocsImport::import()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 4
rs 9.4285
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\Services\GitHub;
10
11
use GrahamCampbell\GitHub\GitHubManager;
12
use Illuminate\Support\Collection;
13
14
/**
15
 * Class GitHubDocsImport.
16
 */
17
class GitHubDocsImport implements DocsImportInterface
18
{
19
    /**
20
     * @var GitHubManager
21
     */
22
    private $manager;
23
24
    /**
25
     * GitHubDocsImport constructor.
26
     * @param GitHubManager   $manager
27
     */
28
    public function __construct(GitHubManager $manager)
29
    {
30
        $this->manager = $manager;
31
    }
32
33
    /**
34
     * @param string $org
35
     * @param string $repo
36
     * @param string $branch
37
     * @return Collection
38
     * @throws \Github\Exception\ErrorException
39
     * @throws \Github\Exception\InvalidArgumentException
40
     * @throws \RuntimeException
41
     */
42
    public function findFiles(string $org, string $repo, string $branch): Collection
43
    {
44
        $result = new Collection();
45
46
        $info = $this->getRepoFilesInfo($org, $repo, $branch);
47
48
        foreach ($info as $page) {
49
            if (!isset($page['path'], $page['download_url'])) {
50
                throw new \RuntimeException('Import failed: "path" or "download_url" arguments required.');
51
            }
52
53
            $result[] = $this->import($org, $repo, $branch, $page['path']);
54
        }
55
56
        return $result;
57
    }
58
59
    /**
60
     * @param string $org
61
     * @param string $repo
62
     * @param string $branch
63
     * @param string $file
64
     * @return string
65
     * @throws \Github\Exception\ErrorException
66
     * @throws \Github\Exception\InvalidArgumentException
67
     */
68
    private function getBody(string $org, string $repo, string $branch, string $file): string
69
    {
70
        return (string)$this->manager
71
            ->repo()
72
            ->contents()
73
            ->download($org, $repo, $file, $branch);
74
    }
75
76
    /**
77
     * @param string $org
78
     * @param string $repo
79
     * @param string $branch
80
     * @return array
81
     */
82
    private function getRepoFilesInfo(string $org, string $repo, string $branch): array
83
    {
84
        return $this->manager
85
            ->repo()
86
            ->contents()
87
            ->show($org, $repo, null, $branch);
88
    }
89
90
    /**
91
     * @param string $org
92
     * @param string $repo
93
     * @param string $branch
94
     * @param string $file
95
     * @return ExternalDocsPage
96
     * @throws \Github\Exception\ErrorException
97
     * @throws \Github\Exception\InvalidArgumentException
98
     */
99
    public function import(string $org, string $repo, string $branch, string $file): ExternalDocsPage
100
    {
101
        $body = $this->getBody($org, $repo, $branch, $file);
102
103
        return new ExternalDocsPage(basename($file, '.md'), $body);
104
    }
105
}