Completed
Push — master ( 01d8d1...58ca4d )
by Simon
01:47
created

SiteConfigExtension::getTomorrow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

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

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...
90
            }
91
        }
92
    }
93
94
}
95