Completed
Push — master ( 29242b...64551a )
by Simon
01:44
created

SiteConfigExtension::updateCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 16
nc 1
nop 1
1
<?php
2
3
namespace Firesphere\PartialUserforms\Extensions;
4
5
use DateInterval;
6
use DateTime;
7
use Firesphere\PartialUserforms\Jobs\PartialSubmissionJob;
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 \SilverStripe\SiteConfig\SiteConfig|\Firesphere\PartialUserforms\Extensions\SiteConfigExtension $owner
24
 * @property boolean $SendDailyEmail
25
 * @property boolean $CleanupAfterSend
26
 * @property string $SendMailTo
27
 */
28
class SiteConfigExtension extends DataExtension
29
{
30
    /**
31
     * @var array
32
     */
33
    private static $db = [
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
34
        'SendDailyEmail'   => 'Boolean(false)',
35
        'CleanupAfterSend' => 'Boolean(false)',
36
        'SendMailTo'       => 'Varchar(255)',
37
        'SendMailFrom'     => 'Varchar(255)',
38
    ];
39
40
    /**
41
     * @param FieldList $fields
42
     */
43
    public function updateCMSFields(FieldList $fields)
44
    {
45
        $fields->addFieldToTab('Root', Tab::create('PartialUserFormSubmissions'));
46
47
        $fields->addFieldsToTab('Root.PartialUserFormSubmissions', [
48
            CheckboxField::create(
49
                'SendDailyEmail',
50
                _t(__CLASS__ . 'SendDailyEmail', 'Send partial submissions daily')
51
            ),
52
            CheckboxField::create(
53
                'CleanupAfterSend',
54
                _t(__CLASS__ . 'CleanupAfterSend', 'Remove partial submissions after sending')
55
            ),
56
            $emailField = TextField::create(
57
                'SendMailTo',
58
                _t(__CLASS__ . 'SendMailTo', 'Email address the partial submissions should be send to')
59
            ),
60
            EmailField::create(
61
                'SendMailFrom',
62
                _t(__CLASS__ . 'SendMailFrom', 'Email address from which the partial submissions should be send')
63
            )
64
        ]);
65
66
        $emailField->setDescription(_t(__CLASS__ . '.EmailDescription', 'Can be a comma separated set of addresses'));
67
    }
68
69
    /**
70
     * @throws \Exception
71
     */
72
    public function onAfterWrite()
73
    {
74
        parent::onAfterWrite();
75
76
        if ($this->owner->SendDailyEmail && !empty($this->owner->SendMailTo)) {
77
            $jobs = QueuedJobDescriptor::get()->filter([
78
                'Implementation'         => PartialSubmissionJob::class,
79
                'StartAfter:GreaterThan' => DBDatetime::now()
80
            ]);
81
            // Only create a new job if there isn't one already
82
            if ((int)$jobs->count() === 0) {
83
                $job = Injector::inst()->get(PartialSubmissionJob::class);
84
                /** @var QueuedJobService $queuedJob */
85
                $queuedJob = Injector::inst()->get(QueuedJobService::class);
86
                $dbDateTime = $this->getTomorrow();
87
                $queuedJob->queueJob($job, $dbDateTime->Format(DBDatetime::ISO_DATETIME));
0 ignored issues
show
Bug introduced by
The method Format does only exist in SilverStripe\ORM\FieldType\DBDatetime, but not in Firesphere\PartialUserfo...ons\SiteConfigExtension.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
88
            }
89
        }
90
    }
91
92
    /**
93
     * @return DBDatetime|static
94
     * @throws \Exception
95
     */
96 View Code Duplication
    protected function getTomorrow()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $dateTime = new DateTime(DBDatetime::now());
99
        $interval = new DateInterval('P1D');
100
        $tomorrow = $dateTime->add($interval);
101
        $dbDateTime = DBDatetime::create();
102
        $dbDateTime->setValue($tomorrow->format('Y-m-d 00:00:00'));
103
104
        return $dbDateTime;
105
    }
106
}
107