ClearCacheEventSubscriber   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getSubscribedEvents() 0 15 2
A postPersist() 0 5 1
A postRemove() 0 5 1
A postUpdate() 0 5 1
A clearCache() 0 13 3
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Doctrine\EventSubscriber;
26
27
use Doctrine\Common\EventSubscriber;
28
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
29
use Doctrine\ORM\Events;
30
use Eccube\Application;
31
32
class ClearCacheEventSubscriber implements EventSubscriber
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
33
{
34
    /**
35
     * @var array 対象のエンティティクラス名(FQDN)
36
     */
37
    private $classes;
38
39
    /**
40
     * @var Application
41
     */
42
    private $app;
43
44
    /**
45
     * @param Application $app
46
     */
47
    public function __construct(Application $app)
48
    {
49
        $this->app = $app;
50
51
        $this->classes = array(
52
            'Eccube\Entity\BaseInfo',
53
            'Eccube\Entity\Category',
54
            'Eccube\Entity\PageLayout',
55
            'Eccube\Entity\Block',
56
            'Eccube\Entity\BlockPosition',
57
        );
58
    }
59
60
    public function getSubscribedEvents()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
61
    {
62
        $options = $this->app['config']['doctrine_cache'];
63
        $clearCache = $options['result_cache']['clear_cache'];
64
65
        if ($clearCache) {
66
            return array(
67
                Events::postPersist,
68
                Events::postRemove,
69
                Events::postUpdate,
70
            );
71
        }
72
        // キャッシュの削除オプションが無効の場合は、イベントを定義せず空の配列を返す.
73
        return array();
74
    }
75
76
    public function postPersist(LifecycleEventArgs $args)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
77
    {
78
        $this->app['monolog']->debug('clear result cache: postPersist');
79
        $this->clearCache($args);
80
    }
81
82
    public function postRemove(LifecycleEventArgs $args)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
83
    {
84
        $this->app['monolog']->debug('clear result cache: postRemove');
85
        $this->clearCache($args);
86
    }
87
88
    public function postUpdate(LifecycleEventArgs $args)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
89
    {
90
        $this->app['monolog']->debug('clear result cache: postUpdate');
91
        $this->clearCache($args);
92
    }
93
94
    protected function clearCache(LifecycleEventArgs $args)
95
    {
96
        $entity = $args->getObject();
97
        $classes = $this->classes;
98
        foreach ($classes as $class) {
99
            if ($entity instanceof $class) {
100
                $this->app['monolog']->debug('clear result cache: '.$class);
101
                $cache = $args->getObjectManager()->getConfiguration()->getResultCacheImpl();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getConfiguration() does only exist in the following implementations of said interface: Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager.

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...
102
                $cache->deleteAll();
103
                break;
104
            }
105
        }
106
    }
107
}
108