SiteConfigExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 12
dl 0
loc 64
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 25 1
A onAfterWrite() 0 19 4
1
<?php
2
3
namespace Firesphere\PartialUserforms\Extensions;
4
5
use Exception;
6
use Firesphere\PartialUserforms\Jobs\PartialSubmissionJob;
7
use Firesphere\PartialUserforms\Services\DateService;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Forms\CheckboxField;
10
use SilverStripe\Forms\EmailField;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\Tab;
13
use SilverStripe\Forms\TextField;
14
use SilverStripe\ORM\DataExtension;
15
use SilverStripe\ORM\FieldType\DBDatetime;
16
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
17
use Symbiote\QueuedJobs\Services\QueuedJobService;
18
19
/**
20
 * Class SiteConfigExtension
21
 *
22
 * @package Firesphere\PartialUserforms\Extensions
23
 * @property SiteConfig|SiteConfigExtension $owner
24
 * @property boolean $SendDailyEmail
25
 * @property boolean $CleanupAfterSend
26
 * @property string $SendMailTo
27
 * @property string $SendMailFrom
28
 */
29
class SiteConfigExtension extends DataExtension
30
{
31
    /**
32
     * @var array
33
     */
34
    private static $db = [
35
        'SendDailyEmail'   => 'Boolean(false)',
36
        'CleanupAfterSend' => 'Boolean(false)',
37
        'SendMailTo'       => 'Varchar(255)',
38
        'SendMailFrom'     => 'Varchar(255)',
39
    ];
40
41
    /**
42
     * @param FieldList $fields
43
     */
44 1
    public function updateCMSFields(FieldList $fields)
45
    {
46 1
        $fields->addFieldToTab('Root', Tab::create('PartialUserFormSubmissions'));
47
48 1
        $fields->addFieldsToTab('Root.PartialUserFormSubmissions', [
49 1
            CheckboxField::create(
50 1
                'SendDailyEmail',
51 1
                _t(__CLASS__ . '.SendDailyEmail', 'Send partial submissions daily')
52
            ),
53 1
            CheckboxField::create(
54 1
                'CleanupAfterSend',
55 1
                _t(__CLASS__ . '.CleanupAfterSend', 'Remove partial submissions after sending')
56
            ),
57 1
            $emailField = TextField::create(
58 1
                'SendMailTo',
59 1
                _t(__CLASS__ . '.SendMailTo', 'Email address the partial submissions should be send to')
60
            ),
61 1
            EmailField::create(
62 1
                'SendMailFrom',
63 1
                _t(__CLASS__ . '.SendMailFrom', 'Email address from which the partial submissions should be send')
64
            )
65
        ]);
66
67 1
        $emailField->setDescription(_t(__CLASS__ . '.EmailDescription', 'Can be a comma separated set of addresses'));
68 1
    }
69
70
    /**
71
     * @throws Exception
72
     */
73 50
    public function onAfterWrite()
74
    {
75 50
        parent::onAfterWrite();
76
77 50
        if ($this->owner->SendDailyEmail && !empty($this->owner->SendMailTo)) {
78 14
            $jobs = QueuedJobDescriptor::get()->filter([
79 14
                'Implementation'         => PartialSubmissionJob::class,
80 14
                'StartAfter:GreaterThan' => DBDatetime::now()
81
            ]);
82
            // Only create a new job if there isn't one already
83 14
            if ((int)$jobs->count() === 0) {
84 13
                $job = Injector::inst()->get(PartialSubmissionJob::class);
85
                /** @var QueuedJobService $queuedJob */
86 13
                $queuedJob = Injector::inst()->get(QueuedJobService::class);
87 13
                $dbDateTime = DateService::getTomorrow();
88 13
                $queuedJob->queueJob($job, $dbDateTime->Format(DBDatetime::ISO_DATETIME));
89
            }
90
        }
91 50
    }
92
}
93