Completed
Pull Request — master (#21)
by Julien
19:28
created

Campaigns::getList()   C

Complexity

Conditions 10
Paths 72

Size

Total Lines 56
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 56
rs 6.7741
cc 10
eloc 25
nc 72
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace EncreInformatique\DoctorSenderApi\Endpoints;
4
5
class Campaigns extends Endpoint
6
{
7
    const STATUS_FINISHED = 'finished';
8
    const DEFAULT_FIELDS = array("name", "amount", "subject", "html", "text", "list_unsubscribe", "send_date", "status", "user_list", "country", "utm_source", "utm_medium", "utm_term", "utm_content", "utm_campaign");
9
10
    /*
11
     * Maximum number of returned campaigns.
12
     * 0 means all campaigns with the indicated conditions
13
     */
14
    const DEFAULT_LIMIT = 10;
15
16
    /*
17
     * Error messages
18
     */
19
    const ERROR_NO_ID = 'no campaign id was provided';
20
21
    /*
22
     * SoapClient $client
23
     */
24
    private $client;
25
26
    /*
27
     * We can retrieve the Campaigns with or without Statistics.
28
     *
29
     * Add statistic extra info 
30
     * [0: none, 1: statistics, 2: statistics without unique values (faster)] 
31
     */
32
    private $withStatistics = 1;
33
34
    public function __construct($client)
35
    {
36
        $this->client = $client;
37
        $this->withStatistics = 1;
38
    }
39
40
    /**
41
     * We get the List of Campaigns.
42
     *
43
     * @param $options
44
     * @return array
45
     */
46
    public function getList($options)
47
    {
48
        $sqlWhere = 'status = \''.self::STATUS_FINISHED.'\'';
49
50
        /*
51
         * We define the Date filters.
52
         */
53
        if (isset($options['date'])) {
54
            if (!empty($options['date']['start'])) {
55
                $endDate = new \DateTime("tomorrow");
56
                if (!empty($options['date']['end'])) {
57
                    $endDate = $options['date']['end'];
58
                }
59
                $sqlWhereDate = 'send_date between \''.$options['date']['start']->format("Y-m-d").'\' and \''.$endDate->format("Y-m-d").'\'';
60
            } elseif (!empty($options['date']['end'])) {
61
                $sqlWhereDate = 'send_date <= \''.$options['date']['end']->format("Y-m-d").'\'';
62
            }
63
64
            if (isset($sqlWhereDate)) {
65
                $sqlWhere.= ' and '.$sqlWhereDate;
66
            }
67
        }
68
69
        /*
70
         * We define the List filters.
71
         */
72
        if (isset($options['list']) && preg_match("`([A-z0-9 _-]+)`", $options['list'])) {
73
            // We clean the string
74
            $options['list'] = trim($options['list']);
75
76
            $sqlWhereList = 'user_list = \''.$options['list'].'\'';
77
78
            $sqlWhere.= ' and '.$sqlWhereList;
79
        }
80
81
        /*
82
         * We define the fields.
83
         */
84
        $fields = self::DEFAULT_FIELDS;
85
        if (isset($options['fields'])) {
86
            $fields = $options['fields'];
87
        }
88
89
        /*
90
         * Override the limit.
91
         */
92
        $limit = self::DEFAULT_LIMIT;
93
        if (isset($options['limit'])) {
94
            $limit = $options['limit'];
95
        }
96
97
        return $this->client->webservice(
98
            'dsCampaignGetAll',
99
            array($sqlWhere, $fields, $this->withStatistics, $limit)
100
        );
101
    }
102
103
    /**
104
     * We get the List of Campaigns.
105
     *
106
     * @param $options
107
     */
108
    public function getOne($options)
109
    {
110
        if (!isset($options['id'])) {
111
            return ['error' => true, 'msg' => self::ERROR_NO_ID];
112
        }
113
        $idCampaign = $options['id'];
114
115
        /*
116
         * We define the fields.
117
         */
118
        $fields = self::DEFAULT_FIELDS;
119
        if (isset($options['fields'])) {
120
            $fields = $options['fields'];
121
        }
122
123
        return $this->client->webservice(
124
            'dsCampaignGet',
125
            array($idCampaign, $fields, $this->withStatistics)
126
        );
127
    }
128
}
129