Completed
Pull Request — master (#16)
by Cesar
01:31
created

SettingsService::getSetting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Service;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Component\HttpFoundation\Session\Session;
7
use Symfony\Component\HttpFoundation\Session\SessionInterface;
8
9
/**
10
 * Class SettingsService
11
 * @package App\Service
12
 */
13
class SettingsService
14
{
15
    /**
16
     * @var string[] Default Settings
17
     */
18
    protected $defaultSettings = [
19
        'settings-customer-currency' => 'EUR',
20
        'settings-customer-locale' => 'en_GB',
21
        'settings-customer-id' => 'CS12.000.AAE',
22
        'settings-customer-name' => 'John',
23
        'settings-customer-family-name' => 'Doe',
24
        'settings-customer-email' => '[email protected]',
25
        'settings-customer-street' => 'Rue Bethancourt 5',
26
        'settings-customer-city' => 'Paris',
27
        'settings-customer-province' => 'Paris',
28
        'settings-customer-zip-code' => '75007',
29
        'settings-customer-country' => 'FR',
30
        'settings-merchant-name' => 'PlayGround Shop',
31
        'settings-merchant-email' => '[email protected]',
32
        'settings-merchant-street' => 'Calle Valencia 34',
33
        'settings-merchant-city' => 'Mahora',
34
        'settings-merchant-province' => 'Albacete',
35
        'settings-merchant-zip-code' => '02002',
36
        'settings-merchant-country' => 'FR',
37
        'settings-item-name' => 'Make me proud! TEE (XXL)',
38
        'settings-item-price' => '18',
39
        'settings-item-shipping' => '5',
40
        'settings-item-sku' => 'TEE-MMP-XXL',
41
        'settings-item-description' => 'A tee that will make you proud and good looking, in an XXL size',
42
        'settings-item-purchase-description' => 'Tee sale: Make me proud! (XXL) SPECIAL OFFER',
43
        'settings-item-tax-name' => 'VAT',
44
        'settings-item-tax-value' => '21',
45
    ];
46
47
    /**
48
     * @var Session
49
     */
50
    public $session;
51
52
    /**
53
     * @var LoggerInterface
54
     */
55
    private $logger;
56
57
    /**
58
     * SettingsService constructor.
59
     * @param SessionInterface $session
60
     * @param LoggerInterface $logger
61
     */
62
    public function __construct(SessionInterface  $session, LoggerInterface $logger)
63
    {
64
        $this->session = $session;
0 ignored issues
show
Documentation Bug introduced by
$session is of type object<Symfony\Component...ssion\SessionInterface>, but the property $session was declared to be of type object<Symfony\Component...dation\Session\Session>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
65
        $this->logger = $logger;
66
    }
67
68
    /**
69
     * @param array $settings
70
     */
71
    public function storeSettings(array $settings): void
72
    {
73
        $this->session->set('settings', $settings);
74
    }
75
76
    /**
77
     * Clear settings to Defaults
78
     */
79
    public function clearSettings(): void
80
    {
81
        $this->session->set('settings', $this->defaultSettings);
82
    }
83
84
    /**
85
     * Check if settings are initialized
86
     * @return bool
87
     */
88
    public function isInitialized(): bool
89
    {
90
        $settings = $this->session->get('settings');
91
        if (is_array($settings)) {
92
            return true;
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * @param string $settingKey
100
     *
101
     * @return string
102
     */
103
    public function getSetting(string $settingKey): ?string
104
    {
105
        $settings = $this->session->get('settings');
106
        if (array_key_exists($settingKey, $settings)) {
107
            return $settings[$settingKey];
108
        }
109
110
        return null;
111
    }
112
}
113