CardCreator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 10
c 4
b 1
f 0
lcom 1
cbo 1
dl 0
loc 86
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A getSvgFilePath() 0 4 1
A getSvgTemplate() 0 4 1
B render() 0 43 3
A escape() 0 4 1
A renderIntoTempfile() 0 6 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