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.
Completed
Push — develop ( cf0abc...328c62 )
by Miguel Angel
02:54
created

BasePublisher::parseContents()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 8.8571
cc 5
eloc 8
nc 7
nop 0
crap 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
11
namespace Trefoil\Publishers;
12
13
use Easybook\Publishers\BasePublisher as EasybookBasePublisher;
14
use Symfony\Component\Finder\Finder;
15
use Trefoil\Util\Toolkit;
16
17
abstract class BasePublisher extends EasybookBasePublisher
18
{
19
20
    /**
21
     * It controls the book publishing workflow for this particular publisher.
22
     */
23 24
    public function publishBook()
24
    {
25 24
        $this->filterContents();
26
        
27 24
        parent::publishBook();
28 24
    }
29
30 24
    public function parseContents()
31
    {
32 24
        parent::parseContents();
33
34
        // Plugins may want to remove some content item.
35
        // Remove items with 'remove' property set to true.
36 24
        $items = array();
37 24
        foreach ($this->app['publishing.items'] as $item) {
38 24
            $include = !isset($item['remove']) || (isset($item['remove']) && !$item['remove']);
39 24
            if ($include) {
40 24
                $items[] = $item;
41 24
            }
42 24
        }
43
44 24
        $this->app['publishing.items'] = $items;
45 24
    }
46
    
47
    /**
48
     * It prepares the book images by copying them into the appropriate
49
     * temporary directory. It also prepares an array with all the images
50
     * data needed later to generate the full ebook contents manifest.
51
     *
52
     * @param string $targetDir The directory where the images are copied.
53
     *
54
     * @throws \RuntimeException
55
     * @return array             Images data needed to create the book manifest.
56
     */
57 24
    protected function prepareBookImages($targetDir)
58
    {
59 24 View Code Duplication
        if (!file_exists($targetDir)) {
60
            throw new \RuntimeException(sprintf(
61
                                            " ERROR: Books images couldn't be copied because \n"
62
                                            . " the given '%s' \n"
63
                                            . " directory doesn't exist.",
64
                                            $targetDir
65
                                        ));
66
        }
67
68 24
        $edition = $this->app['publishing.edition'];
69 24
        $format = Toolkit::getCurrentFormat($this->app);
70
71
        // construct the list of source directories for images.
72
        // they will be used sequentially, so images inside each one will override previous images.
73 24
        $sourceDirs = array();
74
75
        // images into the Resources directory of the <format> directory of the current theme
76
        // (which can be set via command line argument):
77
        //     <current-theme-dir>/<current-theme>/<format>/Resources/images/
78
        // where <current_theme_dir> can be either
79
        //        <trefoil-dir>/app/Resources/Themes/
80
        //         or
81
        //        <the path set with the "--dir" publish command line argument>
82
        // 'Common' format takes precedence
83 24
        $sourceDirs[] = Toolkit::getCurrentResourcesDir($this->app, 'Common') . '/images';
84 24
        $sourceDirs[] = Toolkit::getCurrentResourcesDir($this->app) . '/images';
85
86
        // theme images can be overriden by the book:
87
        //     <book-dir>/Resources/images/
88 24
        $sourceDirs[] = sprintf('%s/images', $this->app['publishing.dir.resources']);
89
        //     <book-dir>/Resources/images/<edition-format>/
90 24
        $sourceDirs[] = sprintf('%s/images/%s', $this->app['publishing.dir.resources'], $format);
91
        //     <book-dir>/Resources/images/<edition-name>/
92 24
        $sourceDirs[] = sprintf('%s/images/%s', $this->app['publishing.dir.resources'], $edition);
93
94
        // the normal book images:
95
        //     <book-dir>/images/
96 24
        $sourceDirs[] = $this->app['publishing.dir.contents'] . '/images';
97
98
        // process each directory in sequence, so each one will override the previously copied images
99 24
        $imagesData = array();
100 24
        $i = 1;
101 24
        foreach ($sourceDirs as $imagesDir) {
102
103 24
            if (file_exists($imagesDir)) {
104
105 9
                $images = Finder::create()
106 9
                                ->files()
107 9
                                ->sortByName()
108 24
                                ->in($imagesDir);
109
110 9
                foreach ($images as $image) {
111
112 8
                    $this->app['filesystem']->copy(
113 8
                                            $image->getPathName(),
114 8
                                            $targetDir . '/' . $image->getFileName(),
115
                                            true // overwrite
116 8
                    );
117
118
                    // The right mediatype for jpeg images is jpeg, not jpg
119 8
                    $mediaType = pathinfo($image->getFilename(), PATHINFO_EXTENSION);
120 8
                    $mediaType = str_replace('jpg', 'jpeg', $mediaType);
121
122 8
                    $imagesData[$image->getFileName()] = array(
123 8
                        'id'        => 'image-' . $i++,
124 8
                        'filePath'  => 'images/' . $image->getFileName(),
125
                        'mediaType' => 'image/' . $mediaType
126 8
                    );
127 9
                }
128 9
            }
129 24
        }
130
131 24
        return $imagesData;
132
    }
133
134
    /**
135
     * Retrieve the custom css file to be used with this book
136
     *
137
     * @return null|string
138
     */
139 24
    protected function getCustomCssFile()
140
    {
141
        // try the text file "style.css"
142 24
        $customCss = $this->app->getCustomTemplate('style.css');
143 24
        if ($customCss) {
144 7
            return $customCss;
145
        }
146
147
        // try the Twig template "style.css.twig"
148 17
        $customCss = $this->app->getCustomTemplate('style.css.twig');
149 17
        if ($customCss) {
150
            return $customCss;
151
        }
152
153 17
        return null;
154
    }
155
    
156
    /**
157
     * Filters out the content items based on certain conditions.
158
     *
159
     * - publising edition: if the item has "editions" array, it will only be included
160
     *   in these editions.
161
     * 
162
     */
163 24
    protected function filterContents()
164
    {
165 24
        $newContents = [];
166
167 24
        $edition = $this->app['publishing.edition'];
168
169
        // by default, all content items are included
170 24
        foreach ($this->app->book('contents') as $itemConfig) {
171
172
            // omit editions not in "editions" array
173 24
            if (isset($itemConfig['editions'])) {
174
                if (!in_array($edition, $itemConfig['editions'])) {
175
                    continue;
176
                }
177 24
            }
178 24
            $newContents[] = $itemConfig;
179 24
        }
180
181 24
        $this->app->book('contents', $newContents);
182 24
    }
183
184
}
185