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.

PdfPublisher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 30.99 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 22
loc 71
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadContents() 0 21 4
A addBookCover() 12 12 2
A getCustomCover() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the trefoil application.
5
 *
6
 * (c) Miguel Angel Gabriel <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Trefoil\Publishers;
13
14
use ZendPdf\PdfDocument;
15
16
/**
17
 * It publishes the book as a PDF file. All the internal links are transformed
18
 * into clickable cross-section book links. These links even display automatically
19
 * the page number where they point into, so no information is lost when printing
20
 * the book.
21
 */
22
abstract class PdfPublisher extends BasePublisher
23
{
24
25
    public function loadContents()
26
    {
27
        parent::loadContents();
28
29
        // if the book includes its own cover as a PDF file,
30
        // remove the default 'cover' element to prevent
31
        // publishing a book with two covers
32
        if (null !== $this->getCustomCover()) {
33
            $bookItems = array();
34
35
            // remove any element of type 'cover' from the
36
            // publishing items
37
            foreach ($this->app['publishing.items'] as $item) {
38
                if ('cover' != $item['config']['element']) {
39
                    $bookItems[] = $item;
40
                }
41
            }
42
43
            $this->app['publishing.items'] = $bookItems;
44
        }
45
    }
46
47
    /**
48
     * If the book defines a custom PDF cover, this method prepends it
49
     * to the PDF book.
50
     *
51
     * @param string $bookFilePath  The path of the original PDF book without the cover
52
     * @param string $coverFilePath The path of the PDF file which will be displayed as the cover of the book
53
     *
54
     * @protected (set to public to be able to unit test it)
55
     */
56 View Code Duplication
    public function addBookCover($bookFilePath, $coverFilePath)
57
    {
58
        if (!empty($coverFilePath)) {
59
            $pdfBook = PdfDocument::load($bookFilePath);
60
            $pdfCover = PdfDocument::load($coverFilePath);
61
62
            $pdfCover = clone $pdfCover->pages[0];
63
            array_unshift($pdfBook->pages, $pdfCover);
64
65
            $pdfBook->save($bookFilePath, true);
66
        }
67
    }
68
69
    /**
70
     * It looks for custom book cover PDF. The search order is:
71
     *   1. <book>/Resources/Templates/<edition-name>/cover.pdf
72
     *   2. <book>/Resources/Templates/pdf/cover.pdf
73
     *   3. <book>/Resources/Templates/cover.pdf
74
     *
75
     * @param string $coverFileName The name of the PDF file that defines the book cover
76
     *
77
     * @return null|string The filePath of the PDF cover or null if none exists
78
     *
79
     * @protected (set to public to be able to unit test it)
80
     */
81 View Code Duplication
    public function getCustomCover($coverFileName = 'cover.pdf')
82
    {
83
        $paths = array(
84
            $this->app['publishing.dir.templates'] . '/' . $this->app['publishing.edition'],
85
            $this->app['publishing.dir.templates'] . '/pdf',
86
            $this->app['publishing.dir.templates'],
87
        );
88
89
        return $this->app->getFirstExistingFile($coverFileName, $paths);
90
    }
91
92
}
93