CardCreator::renderIntoTempfile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace raphiz\passwordcards;
4
5
class CardCreator
6
{
7
    private $configration = null;
8
9
    public function __construct($configration)
10
    {
11
        if ($configration === null) {
12
            throw new \Exception('The given $configuration is null!');
13
        }
14
15
        if ($configration instanceof Configuration === false) {
16
            throw new \Exception(
17
                'The given $configuration is not a valid ' .
18
                'Configuration object.'
19
            );
20
        }
21
22
        $this->configration = $configration;
23
    }
24
25
    public function getSvgFilePath($template_name)
26
    {
27
        return __DIR__ . "/../templates/$template_name.svg";
28
    }
29
30
    public function getSvgTemplate($template_name)
31
    {
32
        return file_get_contents($this->getSvgFilePath($template_name));
33
    }
34
35
    public function render($svg)
36
    {
37
        // Get and count available characters
38
        $chars = $this->configration->getPatternCharacters();
39
        $char_count = count($chars);
40
41
        // set seed
42
        $seed = $this->configration->seed;
43
        mt_srand($seed);
44
45
        $number_of_keys = strlen($this->configration->keys);
46
        for ($i = 0; $i < $number_of_keys; $i++) {
47
            $equivalent = $chars[mt_rand(0, $char_count-1)];
48
49
            $equivalent = $this->escape($equivalent);
50
51
            // Replace the equivalent on the "keyboard"
52
            $svg = str_replace('$' . ($i+1) . '$', $equivalent, $svg);
53
54
            $svg = str_replace('$k' . ($i+1) . '$', $this->configration->keys[$i], $svg);
55
56
        }
57
58
        $space_lenght = $this->configration->spaceBarSize;
0 ignored issues
show
Bug introduced by
The property spaceBarSize does not seem to exist in raphiz\passwordcards\Configuration.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
59
        $space = '';
60
        for ($i = 0; $i < $space_lenght; $i++) {
61
            $space .= $this->escape($chars[mt_rand(0, $char_count-1)]);
62
        }
63
64
        $svg = str_replace('$SPACE$', $space, $svg);
65
66
        $svg = str_replace('$SEED$', $seed, $svg);
67
68
        $svg = str_replace('$PRIMARY$', $this->configration->primaryColor, $svg);
69
70
        $svg = str_replace('$SECONDARY$', $this->configration->secondaryColor, $svg);
71
72
        $svg = str_replace('$TEXT$', $this->escape($this->configration->text), $svg);
73
74
        $svg = str_replace('$PATTERN$', $this->escape($this->configration->pattern), $svg);
75
76
        return $svg;
77
    }
78
    private function escape($str)
79
    {
80
        return htmlentities(utf8_encode($str), ENT_XML1);
81
    }
82
83
84
    public function renderIntoTempfile($svg)
85
    {
86
        $uri = tempnam("/tmp", "php");
87
        file_put_contents($uri, $this->render($svg));
88
        return $uri;
89
    }
90
}
91