Failed Conditions
Push — master ( ee6ce9...9ed6ac )
by Yangsin
485:10 queued 475:23
created

ClearCacheEventSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.56%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 76
ccs 40
cts 41
cp 0.9756
rs 10
c 2
b 0
f 0

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 1059
    public function __construct(Application $app)
48
    {
49 1059
        $this->app = $app;
50
51 1059
        $this->classes = array(
52 1059
            'Eccube\Entity\BaseInfo',
53 1059
            'Eccube\Entity\Category',
54 1059
            'Eccube\Entity\PageLayout',
55 1059
            'Eccube\Entity\Block',
56 1059
            'Eccube\Entity\BlockPosition',
57
        );
58 1059
    }
59
60 1059
    public function getSubscribedEvents()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
61
    {
62 1059
        $options = $this->app['config']['doctrine_cache'];
63 1059
        $clearCache = $options['result_cache']['clear_cache'];
64
65 1059
        if ($clearCache) {
66
            return array(
67 1059
                Events::postPersist,
68 1059
                Events::postRemove,
69 1059
                Events::postUpdate,
70 1059
            );
71
        }
72
        // キャッシュの削除オプションが無効の場合は、イベントを定義せず空の配列を返す.
73
        return array();
74
    }
75
76 640
    public function postPersist(LifecycleEventArgs $args)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
77
    {
78 640
        $this->app['monolog']->debug('clear result cache: postPersist');
79 640
        $this->clearCache($args);
80 640
    }
81
82 107
    public function postRemove(LifecycleEventArgs $args)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
83
    {
84 107
        $this->app['monolog']->debug('clear result cache: postRemove');
85 107
        $this->clearCache($args);
86 107
    }
87
88 393
    public function postUpdate(LifecycleEventArgs $args)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
89
    {
90 393
        $this->app['monolog']->debug('clear result cache: postUpdate');
91 393
        $this->clearCache($args);
92 393
    }
93
94 661
    protected function clearCache(LifecycleEventArgs $args)
95
    {
96 661
        $entity = $args->getObject();
97 661
        $classes = $this->classes;
98 661
        foreach ($classes as $class) {
99 661
            if ($entity instanceof $class) {
100 90
                $this->app['monolog']->debug('clear result cache: '.$class);
101 90
                $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 90
                $cache->deleteAll();
103 90
                break;
104
            }
105 661
        }
106 661
    }
107
}
108