1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BoneMvc\Module\Orc; |
6
|
|
|
|
7
|
|
|
use Barnacle\Container; |
8
|
|
|
use Barnacle\Exception\NotFoundException; |
9
|
|
|
use Barnacle\RegistrationInterface; |
10
|
|
|
use BoneMvc\Module\Orc\Controller\OrcApiController; |
11
|
|
|
use BoneMvc\Module\Orc\Controller\OrcController; |
12
|
|
|
use BoneMvc\Module\Orc\Service\OrcService; |
13
|
|
|
use Bone\Http\Middleware\HalCollection; |
14
|
|
|
use Bone\Http\Middleware\HalEntity; |
15
|
|
|
use Bone\Mvc\Router\RouterConfigInterface; |
16
|
|
|
use Bone\Mvc\View\PlatesEngine; |
17
|
|
|
use Doctrine\ORM\EntityManager; |
18
|
|
|
use League\Route\RouteGroup; |
19
|
|
|
use League\Route\Router; |
20
|
|
|
use League\Route\Strategy\JsonStrategy; |
21
|
|
|
use Zend\Diactoros\ResponseFactory; |
22
|
|
|
|
23
|
|
View Code Duplication |
class OrcPackage implements RegistrationInterface, RouterConfigInterface |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param Container $c |
27
|
|
|
*/ |
28
|
|
|
public function addToContainer(Container $c) |
29
|
|
|
{ |
30
|
|
|
/** @var PlatesEngine $viewEngine */ |
31
|
|
|
$viewEngine = $c->get(PlatesEngine::class); |
32
|
|
|
$viewEngine->addFolder('orc', 'src/Orc/View/Orc/'); |
33
|
|
|
|
34
|
|
|
$c[OrcService::class] = $c->factory(function (Container $c) { |
35
|
|
|
$em = $c->get(EntityManager::class); |
36
|
|
|
return new OrcService($em); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
$c[OrcController::class] = $c->factory(function (Container $c) { |
40
|
|
|
$service = $c->get(OrcService::class); |
41
|
|
|
/** @var PlatesEngine $viewEngine */ |
42
|
|
|
$viewEngine = $c->get(PlatesEngine::class); |
43
|
|
|
|
44
|
|
|
return new OrcController($viewEngine, $service); |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
$c[OrcApiController::class] = $c->factory(function (Container $c) { |
48
|
|
|
$service = $c->get(OrcService::class); |
49
|
|
|
|
50
|
|
|
return new OrcApiController($service); |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getEntityPath(): string |
58
|
|
|
{ |
59
|
|
|
return '/src/Orc/Entity'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function hasEntityPath(): bool |
66
|
|
|
{ |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Container $c |
72
|
|
|
* @param Router $router |
73
|
|
|
* @return Router |
74
|
|
|
*/ |
75
|
|
|
public function addRoutes(Container $c, Router $router): Router |
76
|
|
|
{ |
77
|
|
|
$router->map('GET', '/orc', [OrcController::class, 'indexAction']); |
78
|
|
|
$router->map('GET', '/orc/{id:number}', [OrcController::class, 'viewAction']); |
79
|
|
|
$router->map('GET', '/orc/create', [OrcController::class, 'createAction']); |
80
|
|
|
$router->map('GET', '/orc/edit/{id:number}', [OrcController::class, 'editAction']); |
81
|
|
|
$router->map('GET', '/orc/delete/{id:number}', [OrcController::class, 'deleteAction']); |
82
|
|
|
|
83
|
|
|
$router->map('POST', '/orc/create', [OrcController::class, 'createAction']); |
84
|
|
|
$router->map('POST', '/orc/edit/{id:number}', [OrcController::class, 'editAction']); |
85
|
|
|
$router->map('POST', '/orc/delete/{id:number}', [OrcController::class, 'deleteAction']); |
86
|
|
|
|
87
|
|
|
$factory = new ResponseFactory(); |
88
|
|
|
$strategy = new JsonStrategy($factory); |
89
|
|
|
$strategy->setContainer($c); |
90
|
|
|
|
91
|
|
|
$router->group('/api', function (RouteGroup $route) { |
92
|
|
|
$route->map('GET', '/orc', [OrcApiController::class, 'indexAction'])->prependMiddleware(new HalCollection(5)); |
93
|
|
|
$route->map('GET', '/orc/{id:number}', [OrcApiController::class, 'viewAction'])->prependMiddleware(new HalEntity()); |
94
|
|
|
$route->map('POST', '/orc', [OrcApiController::class, 'createAction']); |
95
|
|
|
$route->map('PUT', '/orc/{id:number}', [OrcApiController::class, 'updateAction']); |
96
|
|
|
$route->map('DELETE', '/orc/{id:number}', [OrcApiController::class, 'deleteAction']); |
97
|
|
|
}) |
98
|
|
|
->setStrategy($strategy); |
99
|
|
|
|
100
|
|
|
return $router; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.