ApplyController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B onDispatch() 0 22 4
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 ApplyController extends AbstractActionController
12
{
13
    /**
14
     * @var Migration
15
     */
16
    protected $migration;
17
18
    /**
19
     * @param Migration $migration
20
     */
21
    public function __construct(Migration $migration)
22
    {
23
        $this->migration = $migration;
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
        $version = $e->getRequest()->getParam('version');
33
        $force = $e->getRequest()->getParam('force');
34
        $down = $e->getRequest()->getParam('down');
35
36
        if (is_null($version) && $force) {
37
            $response = "Can't force migration apply without migration version explicitly set.\n";
38
            $e->setResult($response);
39
            return $response;
40
        }
41
42
        $this->migration->migrate($version, $force, $down);
43
        $response = "Migrations applied!\n";
44
45
        $e->setResult($response);
46
        return $response;
47
    }
48
}
49