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

MigrateController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 11
c 4
b 1
f 0
lcom 2
cbo 3
dl 0
loc 79
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A onDispatch() 0 8 2
A __construct() 0 5 1
B applyAction() 0 17 5
A generateAction() 0 6 1
A getMigration() 0 4 1
A getGenerator() 0 4 1
1
<?php
2
3
namespace T4web\Migrations\Controller;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use T4web\Migrations\Service\Migration;
7
use T4web\Migrations\Service\Generator;
8
use Zend\Console\Request as ConsoleRequest;
9
use Zend\Mvc\MvcEvent;
10
11
/**
12
 * Migration commands controller
13
 */
14
class MigrateController extends AbstractActionController
15
{
16
    /**
17
     * @var Migration
18
     */
19
    protected $migration;
20
21
    /**
22
     * @var Generator
23
     */
24
    protected $generator;
25
26
    public function onDispatch(MvcEvent $e)
27
    {
28
        if (!$this->getRequest() instanceof ConsoleRequest) {
29
            throw new \RuntimeException('You can only use this action from a console!');
30
        }
31
32
        parent::onDispatch($e);
33
    }
34
    /**
35
     * MigrateController constructor.
36
     *
37
     * @param Migration $migration
38
     * @param Generator $generator
39
     */
40
    public function __construct(Migration $migration, Generator $generator)
41
    {
42
        $this->migration = $migration;
43
        $this->generator = $generator;
44
    }
45
46
    /**
47
     * Apply migration
48
     */
49
    public function applyAction()
50
    {
51
        $version = $this->getRequest()->getParam('version');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getParam() does only exist in the following implementations of said interface: Zend\Console\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
52
        $force = $this->getRequest()->getParam('force');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getParam() does only exist in the following implementations of said interface: Zend\Console\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
53
        $down = $this->getRequest()->getParam('down');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getParam() does only exist in the following implementations of said interface: Zend\Console\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
54
        $fake = $this->getRequest()->getParam('fake');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getParam() does only exist in the following implementations of said interface: Zend\Console\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
55
56
        if (is_null($version) && $force) {
57
            return "Can't force migration apply without migration version explicitly set.\n";
58
        }
59
        if (is_null($version) && $fake) {
60
            return "Can't fake migration apply without migration version explicitly set.\n";
61
        }
62
63
        $this->getMigration()->migrate($version, $force, $down, $fake);
64
        return "Migrations applied!\n";
65
    }
66
67
    /**
68
     * Generate new migration skeleton class
69
     */
70
    public function generateAction()
71
    {
72
        $classPath = $this->getGenerator()->generate();
73
74
        return sprintf("Generated skeleton class @ %s\n", realpath($classPath));
75
    }
76
77
    /**
78
     * @return Migration
79
     */
80
    public function getMigration()
81
    {
82
        return $this->migration;
83
    }
84
85
    /**
86
     * @return Generator
87
     */
88
    public function getGenerator()
89
    {
90
        return $this->generator;
91
    }
92
}
93