GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RegisterResources   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 140
ccs 62
cts 66
cp 0.9394
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
A onPrePublish() 0 16 1
A registerOwnPlugins() 0 35 5
A registerEventSubscribers() 0 27 5
A registerPlugin() 0 8 2
A registerOwnThemes() 0 22 2
1
<?php
2
/*
3
 * This file is part of the trefoil application.
4
 *
5
 * (c) Miguel Angel Gabriel <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Trefoil\Bridge\Easybook;
12
13
use Easybook\Events\BaseEvent;
14
use Easybook\Events\EasybookEvents;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
use Symfony\Component\Finder\Finder;
17
use Trefoil\Events\TrefoilEvents;
18
use Trefoil\Util\Toolkit;
19
20
/**
21
 * Register our own resources into easybook
22
 *
23
 */
24
class RegisterResources implements EventSubscriberInterface
25
{
26
    protected $app;
27
    protected $output;
28
29 34
    public static function getSubscribedEvents()
30 34
    {
31 34
        return array(EasybookEvents::PRE_PUBLISH => 'onPrePublish');
32
    }
33
34 34
    public function onPrePublish(BaseEvent $event)
35
    {
36 34
        $this->app = $event->app;
37 34
        $this->output = $event->app['console.output'];
38
39 34
        $this->registerOwnPlugins();
40
41 34
        $this->registerOwnThemes();
42
43
        /** @var \Easybook\DependencyInjection\Application $app */
44 34
        $app = $this->app;
45 34
        $app->dispatch(
46 34
            TrefoilEvents::PRE_PUBLISH_AND_READY,
47 34
            new BaseEvent($app)
48 34
        );
49 34
    }
50
51
    /**
52
     * Register our own plugins
53
     */
54 34
    public function registerOwnPlugins()
55
    {
56
        // register mandatory plugins
57 34
        $this->registerEventSubscribers(__DIR__ . '/../../Plugins', 'Trefoil\\Plugins\\');
58
59
        // register optional plugins
60 34
        $edition = $this->app['publishing.edition'];
61 34
        $bookEditions = $this->app->book('editions');
62 34
        if (!isset($bookEditions[$edition]['plugins'])) {
63
            // no 'plugins' section
64 6
            return;
65
        }
66
67 28
        if (!isset($bookEditions[$edition]['plugins']['enabled'])) {
68
            // no plugins to register
69
            return;
70
        }
71
72 28
        $enabledPlugins = $bookEditions[$edition]['plugins']['enabled'];
73
74 28
        $registered = $this->registerEventSubscribers(
75 28
                           __DIR__ . '/../../Plugins/Optional',
76 28
                           'Trefoil\\Plugins\\Optional\\',
77
                           $enabledPlugins
78 28
        );
79
80
        // tell the user
81 28
        foreach ($enabledPlugins as $plugin) {
82 28
            if (!in_array($plugin, $registered)) {
83
                throw new \Exception(
84
                    'Enabled plugin was not registered: ' . $plugin);
85
            }
86 28
            $this->output->writeLn(sprintf(" > Using plugin %s", $plugin));
87 28
        }
88 28
    }
89
90
    /**
91
     * @param       $dir
92
     * @param       $namespace
93
     * @param array $selectedPlugins List of plugins to register,
94
     *                               or null to register all
95
     *
96
     * @return array
97
     */
98 34
    private function registerEventSubscribers($dir,
99
                                              $namespace,
100
                                              array $selectedPlugins = null)
101
    {
102 34
        if (!file_exists($dir)) {
103
            return array();
104
        }
105
106
        // find and register all plugins in dir
107 34
        $files = Finder::create()->files()->name('*Plugin.php')->depth(0)->in($dir);
108
109 34
        $registered = array();
110 34
        foreach ($files as $file) {
111 34
            $className = $file->getBasename('.php'); // strip .php extension
112
113 34
            $pluginName = $file->getBasename('Plugin.php');
114
115 34
            if ($selectedPlugins !== null && !in_array($pluginName, $selectedPlugins)) {
116 28
                continue;
117
            }
118
119 34
            $this->registerPlugin($namespace, $className);
120 34
            $registered[] = $pluginName;
121 34
        }
122
123 34
        return $registered;
124
    }
125
126
    /**
127
     * Register one plugin.
128
     *
129
     * @param $namespace
130
     * @param $className
131
     */
132 34
    protected function registerPlugin($namespace, $className)
133
    {
134 34
        $r = new \ReflectionClass($namespace . $className);
135
136 34
        if ($r->implementsInterface('Symfony\\Component\\EventDispatcher\\EventSubscriberInterface')) {
137 34
            $this->app->get('dispatcher')->addSubscriber($r->newInstance());
138 34
        }
139 34
    }
140
141 34
    protected function registerOwnThemes()
142
    {
143
        // themes get actually registered in the TwigServiceProvider class
144
        // here we only tell the user what's being used
145
146 34
        $theme = ucfirst($this->app->edition('theme'));
147
148 34
        $themesDir = toolkit::getCurrentThemeDir($this->app);
149 34
        if (!file_exists($themesDir)) {
150 20
            $this->output->writeLn(
151 20
                         sprintf(
152
                             " > <bg=yellow;fg=black> WARNING </> " .
153 20
                             "Theme %s not found in themes directory, assuming default easybook theme",
154
                             $theme
155 20
                         )
156 20
            );
157
158 20
            return;
159
        }
160
161 14
        $this->output->writeLn(sprintf(" > Using theme  %s from %s", $theme, $themesDir));
162 14
    }
163
}
164