zend-blog-3-backend

Форк
0
/
SystemParametersStorage.php 
113 строк · 2.4 Кб
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: morontt
5
 * Date: 07.10.17
6
 * Time: 11:04
7
 */
8

9
namespace App\Service;
10

11
use App\Entity\SystemParameters;
12
use App\Repository\SystemParametersRepository;
13
use Doctrine\ORM\EntityManager;
14
use Doctrine\ORM\EntityManagerInterface;
15
use Doctrine\ORM\ORMException;
16

17
class SystemParametersStorage
18
{
19
    const CIPHER = 'DES-EDE3-OFB';
20

21
    /**
22
     * @var SystemParametersRepository
23
     */
24
    protected $parametersRepo;
25

26
    /**
27
     * @var string
28
     */
29
    protected $secret;
30

31
    /**
32
     * @var EntityManager
33
     */
34
    protected $em;
35

36
    /**
37
     * @param EntityManagerInterface $em
38
     * @param string $secret
39
     */
40
    public function __construct(EntityManagerInterface $em, string $secret)
41
    {
42
        $this->parametersRepo = $em->getRepository(SystemParameters::class);
43
        $this->em = $em;
44

45
        $this->secret = $secret;
46
    }
47

48
    /**
49
     * @param string $key
50
     * @param string $value
51
     * @param bool $encrypted
52
     *
53
     * @throws ORMException
54
     */
55
    public function saveParameter(string $key, string $value, bool $encrypted = false)
56
    {
57
        $sp = $this->parametersRepo->findOneByOptionKey($key);
58

59
        if (!$sp) {
60
            $sp = new SystemParameters();
61
            $sp->setOptionKey($key);
62
            $this->em->persist($sp);
63
        }
64

65
        $sp->setValue($encrypted ? $this->encrypt($value) : $value)
66
            ->setEncrypted($encrypted);
67
        $this->em->flush();
68
    }
69

70
    /**
71
     * @param string $key
72
     *
73
     * @return string|null
74
     */
75
    public function getParameter(string $key): ?string
76
    {
77
        $sp = $this->parametersRepo->findOneByOptionKey($key);
78

79
        if (!$sp) {
80
            return null;
81
        }
82

83
        return $sp->isEncrypted() ? $this->decrypt($sp->getValue()) : $sp->getValue();
84
    }
85

86
    /**
87
     * @param string $value
88
     *
89
     * @return string
90
     */
91
    public function encrypt(string $value): string
92
    {
93
        return base64_encode(openssl_encrypt($value, self::CIPHER, $this->secret, 0, $this->getVector()));
94
    }
95

96
    /**
97
     * @param string $value
98
     *
99
     * @return string
100
     */
101
    public function decrypt(string $value): string
102
    {
103
        return openssl_decrypt(base64_decode($value), self::CIPHER, $this->secret, 0, $this->getVector());
104
    }
105

106
    /**
107
     * @return string
108
     */
109
    protected function getVector(): string
110
    {
111
        return substr(sha1($this->secret), 0, openssl_cipher_iv_length(self::CIPHER));
112
    }
113
}
114

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.