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.

EpubCheckPlugin   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 68
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 7 1
A onPostPublish() 0 11 2
B bookCheck() 0 44 5
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
namespace Trefoil\Plugins\Optional;
11
12
use Easybook\Events\BaseEvent;
13
use Easybook\Events\EasybookEvents;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\Process\Process;
16
use Trefoil\Plugins\BasePlugin;
17
/**
18
 * Plugin to check the generated epub ebook using the EpubCheck utility.
19
 *
20
 * @see https://github.com/IDPF/epubcheck
21
 *
22
 * For formats: Epub
23
 *
24
 * Options can be set in the book's config.yml:
25
 *
26
 *    easybook:
27
 *        parameters:
28
 *            epubcheck.path: '/path/to/epubcheck.jar'
29
 *            epubcheck.command_options: ''
30
 *
31
 */
32
class EpubCheckPlugin extends BasePlugin implements EventSubscriberInterface
33
{
34
35
    public static function getSubscribedEvents()
36
    {
37
        return array(
38
            // runs later but before renaming
39
            EasybookEvents::POST_PUBLISH => array('onPostPublish', -900)
40
        );
41
    }
42
43
    public function onPostPublish(BaseEvent $event)
44
    {
45
        $this->init($event);
46
47
        if ($this->format != 'Epub') {
48
            // not for this format
49
            return;
50
        }
51
52
        $this->bookCheck();
53
    }
54
55
    protected function bookCheck()
56
    {
57
        $epubcheck = $this->getConfigOption('easybook.parameters.epubcheck.path');
58
        $epubcheckOptions = $this->getConfigOption('easybook.parameters.epubcheck.command_options');
59
60
        if (!$epubcheck || !file_exists($epubcheck)) {
61
            $this->writeLn(
62
                 'The EpubCheck library needed to check EPUB books cannot be found. ' .
63
                 'Check that you have set your custom Epubcheck path in the book\'s config.yml file.',
64
                 'error'
65
            );
66
67
            return;
68
        }
69
70
        $epubFilePath = $this->app['publishing.dir.output'] . '/book.epub';
71
72
        $command = sprintf(
73
            "java -jar '%s' '%s' %s",
74
            $epubcheck,
75
            $epubFilePath,
76
            $epubcheckOptions
77
        );
78
79
        $this->writeLn('Running EpubCheck...');
80
81
        $process = new Process($command);
82
        $process->run();
83
84
        $outputText = $process->getOutput() . $process->getErrorOutput();
85
86
        if ($process->isSuccessful()) {
87
            $this->writeLn('No errors');
88
        } else {
89
            if (preg_match('/^ERROR:/Um', $outputText)) {
90
                $this->writeLn('Some errors detected.</error>', 'error');
91
            } else {
92
                $this->writeLn('Some warnings detected.', "warning");
93
            }
94
        }
95
96
        $reportFile = $this->app['publishing.dir.output'] . '/report-EpubCheckPlugin.txt';
97
        file_put_contents($reportFile, $outputText);
98
    }
99
}
100