getCleanupAfterDateString()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * File:ExpiredPasswordCleaner.php
6
 *
7
 * @author Maciej Sławik <[email protected]>
8
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
9
 */
10
11
namespace LizardMedia\PasswordMigrator\Model;
12
13
use Exception;
14
use LizardMedia\PasswordMigrator\Api\Config\ConfigProviderInterface;
15
use LizardMedia\PasswordMigrator\Api\Data\PasswordRepositoryInterface;
16
use LizardMedia\PasswordMigrator\Api\ExpiredPasswordCleanerInterface;
17
use LizardMedia\PasswordMigrator\Exception\CleanupDisabledException;
18
use LizardMedia\PasswordMigrator\Model\Config\Source\Cleanup;
19
use Magento\Framework\Stdlib\DateTime\DateTime;
20
21
/**
22
 * Class ExpiredPasswordCleaner
23
 * @package LizardMedia\PasswordMigrator\Model
24
 */
25
class ExpiredPasswordCleaner implements ExpiredPasswordCleanerInterface
26
{
27
    /**
28
     * @var PasswordRepositoryInterface
29
     */
30
    private $passwordRepository;
31
32
    /**
33
     * @var ConfigProviderInterface
34
     */
35
    private $configProvider;
36
37
    /**
38
     * @var DateTime
39
     */
40
    private $dateTime;
41
42
    /**
43
     * ExpiredPasswordCleaner constructor.
44
     * @param PasswordRepositoryInterface $passwordRepository
45
     * @param ConfigProviderInterface $configProvider
46
     * @param DateTime $dateTime
47
     */
48
    public function __construct(
49
        PasswordRepositoryInterface $passwordRepository,
50
        ConfigProviderInterface $configProvider,
51
        DateTime $dateTime
52
    ) {
53
        $this->passwordRepository = $passwordRepository;
54
        $this->configProvider = $configProvider;
55
        $this->dateTime = $dateTime;
56
    }
57
58
    /**
59
     * @return void
60
     */
61
    public function removeExpiredPasswords(): void
62
    {
63
        try {
64
            $cleanupSinceDate = $this->getCleanupAfterDateString();
65
            $passwordsToRemove = $this->passwordRepository->getOlderThan($cleanupSinceDate);
66
            foreach ($passwordsToRemove as $password) {
67
                $this->passwordRepository->delete($password);
68
            }
69
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
70
        }
71
    }
72
73
    /**
74
     * @return string
75
     * @throws CleanupDisabledException
76
     */
77
    private function getCleanupAfterDateString(): string
78
    {
79
        $cleanupAfter = $this->configProvider->getCleanupAfter();
80
        switch ($cleanupAfter) {
81
            case Cleanup::CLEAN_YEAR:
82
                $diff = '-1 year';
83
                break;
84
            case Cleanup::CLEAN_HALFYEAR:
85
                $diff = '-6 months';
86
                break;
87
            default:
88
                throw new CleanupDisabledException(__('Automatic cleanup disabled'));
89
        }
90
91
        return date('Y-m-d', strtotime("{$this->dateTime->date()} {$diff}"));
92
    }
93
}
94