Completed
Push — feature/middleware ( e5c34d...47ade2 )
by Derek Stephen
04:05
created

DragonPackage::addRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace BoneMvc\Module\Dragon;
4
5
use Bone\Mvc\Router\RouterConfigInterface;
6
use BoneMvc\Module\Dragon\Controller\DragonController;
7
use BoneMvc\Module\Dragon\Service\DragonService;
8
use Barnacle\RegistrationInterface;
9
use Doctrine\ORM\EntityManager;
10
use Barnacle\Container;
11
use League\Route\RouteGroup;
12
use League\Route\Router;
13
use League\Route\Strategy\JsonStrategy;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Zend\Diactoros\Response;
17
use Zend\Diactoros\ResponseFactory;
18
19
class DragonPackage implements RegistrationInterface, RouterConfigInterface
20
{
21
    /**
22
     * @param Container $c
23
     */
24
    public function addToContainer(Container $c)
25
    {
26
        $c[DragonService::class] = $c->factory(function (Container $c) {
27
            $em =  $c->get(EntityManager::class);
28
            $service = new DragonService($em);
29
30
            return $service;
31
        });
32
33
        $c[DragonController::class] = $c->factory(function (Container $c) {
34
            $service = $c->get(DragonService::class);
35
            $controller = new DragonController($service);
36
37
            return $controller;
38
        });
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getEntityPath(): string
45
    {
46
        return 'src/Dragon/Entity';
47
    }
48
49
    /**
50
     * @return bool
51
     */
52
    public function hasEntityPath(): bool
53
    {
54
        return true;
55
    }
56
57
    public function addRoutes(Container $c, Router $router)
58
    {
59
        $router->map('GET', '/dragon', [DragonController::class, 'indexAction']);
60
        $router->map('GET', '/dragon/{id}', [DragonController::class, 'indexAction']);
61
62
        $factory = new ResponseFactory();
63
        $strategy = new JsonStrategy($factory);
64
        $strategy->setContainer($c);
65
66
        $router->group('/api', function (RouteGroup $route) {
67
            $route->map('GET', '/dragon', [DragonController::class, 'indexAction']);
68
            $route->map('GET', '/dragon/{id}', [DragonController::class, 'indexAction']);
69
        })
70
        ->setStrategy($strategy);
71
72
        return $router;
73
    }
74
}
75