ListController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 6
c 4
b 0
f 1
lcom 1
cbo 5
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B onDispatch() 0 18 5
A __construct() 0 4 1
1
<?php
2
3
namespace T4web\Migrations\Controller;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use Zend\Console\Request as ConsoleRequest;
7
use Zend\Mvc\MvcEvent;
8
use T4web\Migrations\Version\Resolver;
9
use T4web\Migrations\Exception\RuntimeException;
10
11
class ListController extends AbstractActionController
12
{
13
    /**
14
     * @var Resolver
15
     */
16
    protected $versionResolver;
17
18
    /**
19
     * @param Resolver $versionResolver
20
     */
21
    public function __construct(Resolver $versionResolver)
22
    {
23
        $this->versionResolver = $versionResolver;
24
    }
25
26
    public function onDispatch(MvcEvent $e)
27
    {
28
        if (!$e->getRequest() instanceof ConsoleRequest) {
29
            throw new RuntimeException('You can only use this action from a console!');
30
        }
31
32
        $migrations = $this->versionResolver->getAll($e->getRequest()->getParam('all'));
33
        $list = [];
34
        foreach ($migrations as $m) {
35
            $list[] = sprintf("%s %s - %s", $m['applied'] ? '-' : '+', $m['version'], $m['description']);
36
        }
37
38
        $response = (empty($list) ? 'No migrations available.' : implode("\n", $list)) . "\n";
39
40
        $e->setResult($response);
41
42
        return $response;
43
    }
44
}
45