Completed
Push — master ( 168ecb...bdc00f )
by max
02:03
created

ListController::onDispatch()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 8.8571
cc 5
eloc 10
nc 5
nop 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\Service\Migration;
9
use T4web\Migrations\Exception\RuntimeException;
10
11
class ListController extends AbstractActionController
12
{
13
    /**
14
     * @var Migration
15
     */
16
    protected $migration;
17
18
    /**
19
     * MigrateController constructor.
20
     *
21
     * @param Migration $migration
22
     */
23
    public function __construct(Migration $migration)
24
    {
25
        $this->migration = $migration;
26
    }
27
28
    public function onDispatch(MvcEvent $e)
29
    {
30
        if (!$e->getRequest() instanceof ConsoleRequest) {
31
            throw new RuntimeException('You can only use this action from a console!');
32
        }
33
34
        $migrations = $this->migration->getMigrationClasses($e->getRequest()->getParam('all'));
35
        $list = [];
36
        foreach ($migrations as $m) {
37
            $list[] = sprintf("%s %s - %s", $m['applied'] ? '-' : '+', $m['version'], $m['description']);
38
        }
39
40
        $response = (empty($list) ? 'No migrations available.' : implode("\n", $list)) . "\n";
41
42
        $e->setResult($response);
43
44
        return $response;
45
    }
46
}
47