Completed
Push — develop ( e53a3e...2fad4a )
by greg
08:09
created

SwitchLocaleController::switchAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
/**
3
 * Zend Framework (http://framework.zend.com/)
4
 *
5
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
6
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7
 * @license   http://framework.zend.com/license/new-bsd New BSD License
8
 */
9
10
namespace PlaygroundCore\Controller\Frontend;
11
12
use Zend\ServiceManager\ServiceLocatorAwareInterface;
13
use Zend\ServiceManager\ServiceLocatorInterface;
14
use Zend\Mvc\Controller\AbstractActionController;
15
16
class SwitchLocaleController extends AbstractActionController implements ServiceLocatorAwareInterface
17
{
18
    /**
19
    * @var $localeService : Service des locales
20
    */
21
    protected $localeService;
22
23
    /**
24
    * switchAction : permet de switcher de langue en fonction d'un context (back/front)
25
    * locale : locale pour switch
26
    * context : (back/front)
27
    * referer : retour à la page
28
    *
29
    * @return Redirect $redirect redirect to referer
30
    */
31
    public function switchAction()
32
    {
33
        $locale = $this->getEvent()->getRouteMatch()->getParam('locale');
34
        $context = $this->getEvent()->getRouteMatch()->getParam('area');
35
        $redirect = (!empty($this->getEvent()->getRouteMatch()->getParam('redirect')))? urldecode($this->getEvent()->getRouteMatch()->getParam('redirect')) : '/';
36
37
        $cookie = new \Zend\Http\Header\SetCookie('pg_locale_'.$context, $locale, time() + 60*60*24*365, '/');
38
        $this->getResponse()->getHeaders()->addHeader($cookie);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\ResponseInterface as the method getHeaders() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Response, Zend\Http\Response, Zend\Http\Response\Stream.

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...
39
40
        return $this->redirect()->toUrl($redirect); 
41
    }
42
43
44
    /**
45
    * getServiceLocator : Recuperer le service locator
46
    * @return ServiceLocator $serviceLocator
47
    */
48
    public function getServiceLocator()
49
    {
50
        return $this->serviceLocator;
51
    }
52
53
    /**
54
    * setServiceLocator : set le service locator
55
    */
56
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
57
    {
58
        $this->serviceLocator = $serviceLocator;
59
    }
60
61
      /**
62
    * getLocaleService : Recuperer le service des locales
63
    *
64
    * @return Service/Locale $localeService
0 ignored issues
show
Documentation introduced by
The doc-type Service/Locale could not be parsed: Unknown type name "Service/Locale" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
65
    */
66 View Code Duplication
    public function getLocaleService()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        if ($this->localeService === null) {
69
            $this->localeService = $this->getServiceLocator()->get('playgroundcore_locale_service');
70
        }
71
        return $this->localeService;
72
    }
73
}
74