RepositoryController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 9
dl 104
loc 104
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addOrUpdateRepository() 32 32 4
A get() 16 16 3
A add() 3 3 1
A update() 3 3 1
A delete() 16 16 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Satis\Exceptions\PackageBuildFailedException;
6
use App\Satis\Exceptions\RepositoryNotFoundException;
7
use App\Satis\Model\Repository;
8
use Illuminate\Support\Collection;
9
use Validator;
10
use Illuminate\Http\Request;
11
use App\Satis\ConfigManager;
12
use Response;
13
use Illuminate\Routing\Controller as BaseController;
14
15
/**
16
 * @author Lukas Homza <[email protected]>
17
 */
18 View Code Duplication
class RepositoryController extends BaseController {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    /**
20
     * @param \Illuminate\Http\Request $request
21
     * @param \App\Satis\ConfigManager $configManager
22
     * @param string|null $repositoryId
23
     *
24
     * @throws \HttpInvalidParamException
25
     */
26
    protected function addOrUpdateRepository(Request $request, ConfigManager $configManager, $repositoryId = null) {
27
        $input = new Collection($request->all());
28
29
        $validator = Validator::make(
30
            $request->all(),
31
            ['url' => 'required'],
32
            [
33
                'url.required' => 'Repository url must be provided.'
34
            ]
35
        );
36
37
        if($validator->fails()) {
38
            Response::json($validator->errors()->first('url'))
39
                ->setStatusCode(400)
40
                ->send();
41
        } else {
42
            try {
43
                $output = $configManager->addOrUpdateRepository($repositoryId, $input);
44
45
                Response::json(['command_output' => $output])
46
                    ->send();
47
            } catch(PackageBuildFailedException $e) {
48
                Response::json()
49
                    ->setStatusCode(500)
50
                    ->send();
51
            } catch(RepositoryNotFoundException $e) {
52
                Response::json()
53
                    ->setStatusCode(404)
54
                    ->send();
55
            }
56
        }
57
    }
58
59
    /**
60
     * @param \App\Satis\ConfigManager $configManager
61
     * @param string|null $repositoryId
62
     */
63
    public function get(ConfigManager $configManager, $repositoryId = null) {
64
        try {
65
            $repositories = $configManager->getRepositories($repositoryId);
66
67
            Response::make($repositories)
68
              ->send();
69
        } catch(RepositoryNotFoundException $e) {
70
            Response::json()
71
                ->setStatusCode(404)
72
                ->send();
73
        } catch(PackageBuildFailedException $e) {
74
            Response::json($e->getMessage())
75
                ->setStatusCode(500)
76
                ->send();
77
        }
78
    }
79
80
    /**
81
     * @param \Illuminate\Http\Request $request
82
     * @param \App\Satis\ConfigManager $configManager
83
     *
84
     * @throws \HttpInvalidParamException
85
     */
86
    public function add(Request $request, ConfigManager $configManager) {
87
        $this->addOrUpdateRepository($request, $configManager, null);
88
    }
89
90
    /**
91
     * @param \Illuminate\Http\Request $request
92
     * @param \App\Satis\ConfigManager $configManager
93
     * @param string $repositoryId
94
     *
95
     * @throws \HttpInvalidParamException
96
     */
97
    public function update(Request $request, ConfigManager $configManager, $repositoryId) {
98
        $this->addOrUpdateRepository($request, $configManager, $repositoryId);
99
    }
100
101
    /**
102
     * @param \App\Satis\ConfigManager $configManager
103
     * @param string $repositoryId
104
     */
105
    public function delete(ConfigManager $configManager, $repositoryId) {
106
        try {
107
            $configManager->deleteRepository($repositoryId);
108
109
            Response::json()
110
                ->send();
111
        } catch(RepositoryNotFoundException $e) {
112
            Response::json()
113
                ->setStatusCode(404)
114
                ->send();
115
        } catch(PackageBuildFailedException $e) {
116
            Response::json($e->getMessage())
117
                ->setStatusCode(500)
118
                ->send();
119
        }
120
	}
121
}
122