KnpSnappyAutodetectBundle::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
3
namespace EmanueleMinotto\KnpSnappyAutodetectBundle;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\HttpKernel\Bundle\Bundle;
7
8
/**
9
 * Symfony Bundle for KnpSnappy autodetect.
10
 *
11
 * @author Emanuele Minotto <[email protected]>
12
 */
13
class KnpSnappyAutodetectBundle extends Bundle
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 9
    public function build(ContainerBuilder $container)
19
    {
20 9
        $prefix = '%kernel.root_dir%/../vendor/';
21 9
        $container->prependExtensionConfig('knp_snappy', [
22
            'pdf' => [
23 9
                'binary' => $prefix.$this->getPdfBinary(),
24 9
            ],
25
            'image' => [
26 9
                'binary' => $prefix.$this->getImageBinary(),
27 9
            ],
28 9
        ]);
29 9
    }
30
31
    /**
32
     * Gets wkhtmltoimage binary based on operating system and architecture.
33
     *
34
     * @return string
35
     */
36 9 View Code Duplication
    private function getImageBinary()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38 9
        $os = $this->detectOs();
39
40 9
        if ('windows' === $os) {
41
            return 'wemersonjanuario/wkhtmltox-windows-64bit/bin/wkhtmltoimage.exe';
42
        }
43 9
        if ('mac' === $os) {
44
            return 'messagedigital/wkhtmltoimage-osx/bin/wkhtmltoimage-osx';
45
        }
46
47 9
        $architecture = $this->detectArchitecture();
48
49 9
        return sprintf(
50 9
            'h4cc/wkhtmltoimage-%s/bin/wkhtmltoimage-%s',
51 9
            $architecture,
52
            $architecture
53 9
        );
54
    }
55
56
    /**
57
     * Gets wkhtmltopdf binary based on operating system and architecture.
58
     *
59
     * @return string
60
     */
61 9 View Code Duplication
    private function getPdfBinary()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63 9
        $os = $this->detectOs();
64
65 9
        if ('windows' === $os) {
66
            return 'wemersonjanuario/wkhtmltox-windows-64bit/bin/wkhtmltopdf.exe';
67
        }
68 9
        if ('mac' === $os) {
69
            return 'messagedigital/wkhtmltopdf-osx/bin/wkhtmltopdf-osx';
70
        }
71
72 9
        $architecture = $this->detectArchitecture();
73
74 9
        return sprintf(
75 9
            'h4cc/wkhtmltopdf-%s/bin/wkhtmltopdf-%s',
76 9
            $architecture,
77
            $architecture
78 9
        );
79
    }
80
81
    /**
82
     * Detects common operating systems.
83
     *
84
     * @return string
85
     */
86 9
    private function detectOs()
87
    {
88 9
        if ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) {
89
            return 'windows';
90
        }
91
92 9
        if ('Darwin' === PHP_OS) {
93
            return 'mac';
94
        }
95
96 9
        return 'linux';
97
    }
98
99
    /**
100
     * Detects common architecture types.
101
     *
102
     * @return string
103
     */
104 9
    private function detectArchitecture()
105
    {
106 9
        $check = 123234234234234 % 134;
107 9
        if (-14 == $check || -1 == $check) {
108
            return 'i386';
109
        }
110
111 9
        return 'amd64';
112
    }
113
}
114