@@ 16-69 (lines=54) @@ | ||
13 | ||
14 | use AppBundle\Entity\Project; |
|
15 | ||
16 | class Bitbucket implements RepositoryManager |
|
17 | { |
|
18 | /** |
|
19 | * @var \Bitbucket\API\Repositories\Src |
|
20 | */ |
|
21 | private $client; |
|
22 | ||
23 | /** |
|
24 | * Constructor. |
|
25 | */ |
|
26 | public function __construct() |
|
27 | { |
|
28 | $this->client = new \Bitbucket\API\Repositories\Src(); |
|
29 | //$this->client ->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) ); |
|
30 | } |
|
31 | ||
32 | /** |
|
33 | * Get the file contents. |
|
34 | * |
|
35 | * |
|
36 | * @param Project $project |
|
37 | * @param string $file |
|
38 | * |
|
39 | * @return array |
|
40 | */ |
|
41 | public function getFileContents(Project $project, string $file): array |
|
42 | { |
|
43 | $response = $this->client->raw( |
|
44 | $project->getVendorName(), |
|
45 | $project->getPackageName(), |
|
46 | $project->getBranch(), |
|
47 | $file |
|
48 | ); |
|
49 | ||
50 | return $this->parseJson($response->getContent()); |
|
51 | } |
|
52 | ||
53 | /** |
|
54 | * Parse JSON data. |
|
55 | * |
|
56 | * @param string $data |
|
57 | * |
|
58 | * @return mixed |
|
59 | */ |
|
60 | private function parseJson($data) |
|
61 | { |
|
62 | $parsedData = json_decode($data, true); |
|
63 | if ($parsedData === false) { |
|
64 | throw new \RuntimeException('Unable to parse json file'); |
|
65 | } |
|
66 | ||
67 | return $parsedData; |
|
68 | } |
|
69 | } |
|
70 |
@@ 16-72 (lines=57) @@ | ||
13 | ||
14 | use AppBundle\Entity\Project; |
|
15 | ||
16 | class Github implements RepositoryManager |
|
17 | { |
|
18 | /** |
|
19 | * @var \Github\Client |
|
20 | */ |
|
21 | private $client; |
|
22 | ||
23 | /** |
|
24 | * Constructor. |
|
25 | */ |
|
26 | public function __construct() |
|
27 | { |
|
28 | $this->client = new \Github\Client(); |
|
29 | } |
|
30 | ||
31 | /** |
|
32 | * Get the file contents. |
|
33 | * |
|
34 | * |
|
35 | * @param Project $project |
|
36 | * @param string $file |
|
37 | * |
|
38 | * @return array |
|
39 | */ |
|
40 | public function getFileContents(Project $project, string $file): array |
|
41 | { |
|
42 | try { |
|
43 | $fileContent = $this->client->api('repo')->contents()->download( |
|
44 | $project->getVendorName(), |
|
45 | $project->getPackageName(), |
|
46 | $file, |
|
47 | $project->getBranch() |
|
48 | ); |
|
49 | ||
50 | return $this->parseJson($fileContent); |
|
51 | } catch (\Github\Exception\RuntimeException $e) { |
|
52 | return []; |
|
53 | } |
|
54 | } |
|
55 | ||
56 | /** |
|
57 | * Parse JSON data. |
|
58 | * |
|
59 | * @param string $data |
|
60 | * |
|
61 | * @return mixed |
|
62 | */ |
|
63 | private function parseJson($data) |
|
64 | { |
|
65 | $parsedData = json_decode($data, true); |
|
66 | if ($parsedData === false) { |
|
67 | throw new \RuntimeException('Unable to parse json file'); |
|
68 | } |
|
69 | ||
70 | return $parsedData; |
|
71 | } |
|
72 | } |
|
73 |