Completed
Push — master ( 6d28c9...f75ab2 )
by Jason
02:39
created

ShortURL::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 41
ccs 24
cts 24
cp 1
crap 1
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\ShortURL\Model;
4
5
use Hpatoio\Bitly\Client;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\ReadonlyField;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\ValidationResult;
11
12
class ShortURL extends DataObject
13
{
14
    /**
15
     * @var string
16
     */
17
    private static $singular_name = 'Short URL';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
18
19
    /**
20
     * @var string
21
     */
22
    private static $plural_name = 'Short URLs';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
23
24
    /**
25
     * @var
26
     */
27
    private $bitly_token;
0 ignored issues
show
introduced by
The private property $bitly_token is not used, and could be removed.
Loading history...
28
29
    /**
30
     * @var array
31
     */
32
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
33
        'Title' => 'Varchar(255)',
34
        'URL' => 'Varchar(255)',
35
        'CampaignSource' => 'Varchar(255)',
36
        'CampaignMedium' => 'Varchar(255)',
37
        'CampaignName' => 'Varchar(255)',
38
        'CampaignTerm' => 'Varchar(255)',
39
        'CampaignContent' => 'Varchar(255)',
40
        'ShortURL' => 'Varchar(255)',
41
    ];
42
43
    /**
44
     * @var array
45
     */
46
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
47
        'Title',
48
        'URL',
49
        'ShortURL',
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
56
        'Title',
57
        'URL',
58
        'CampaignSource',
59
        'CampaignMedium',
60
        'CampaignName',
61
        'CampaignTerm',
62
        'CampaignContent',
63
        'ShortURL',
64
    ];
65
66
    /**
67
     * @var string
68
     */
69
    private static $table_name = 'ShortURL';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
70
71
    /**
72
     * @return FieldList
73
     */
74 1
    public function getCMSFields()
75
    {
76 1
        $fields = parent::getCMSFields();
77
78 1
        $fields->dataFieldByName('URL')
79 1
            ->setDescription('URL to shorten and tag');
80
81 1
        $fields->dataFieldByName('CampaignSource')
82 1
            ->setDescription(
83 1
                'Use to identify a search engine, newsletter name, or other source. Example: google'
84
            );
85
86 1
        $fields->dataFieldByName('CampaignMedium')
87 1
            ->setDescription(
88 1
                'Use utm_medium to identify a medium such as email or cost-per- click. Example: cpc'
89
            );
90
91 1
        $fields->dataFieldByName('CampaignName')
92 1
            ->setDescription(
93 1
                'Used for keyword analysis. Use utm_campaign to identify a specific product promotion or 
94
                strategic campaign. Example: utm_campaign=spring_sale'
95
            );
96
97 1
        $fields->dataFieldByName('CampaignTerm')
98 1
            ->setDescription(
99 1
                'Used for paid search. Use utm_term to note the keywords for this ad. Example: running+shoes'
100
            );
101
102 1
        $fields->dataFieldByName('CampaignContent')
103 1
            ->setDescription(
104 1
                'Used for A/B testing and content-targeted ads. Use utm_content to differentiate ads or links 
105
                that point to the same URL. Examples: logolink or textlink'
106
            );
107
108 1
        $fields->addFieldToTab('Root.Main', ReadonlyField::create('LongURL', 'Long URL', $this->getLongURL()));
109
110 1
        $short = $fields->dataFieldByName('ShortURL');
111 1
        $short = $short->performReadonlyTransformation();
112 1
        $fields->addFieldToTab('Root.Main', $short);
113
114 1
        return $fields;
115
    }
116
117
    /**
118
     * @return ValidationResult
119
     */
120 1
    public function validate()
121
    {
122 1
        $result = parent::validate();
123
124 1
        if (!$this->Title) {
125 1
            $result->addError('A Title is required before you can save');
126
        }
127
128 1
        if (!$this->URL) {
0 ignored issues
show
Bug Best Practice introduced by
The property URL does not exist on Dynamic\ShortURL\Model\ShortURL. Since you implemented __get, consider adding a @property annotation.
Loading history...
129
            $result->addError('A URL is required before you can save');
130
        }
131
132 1
        if (!$this->CampaignSource) {
0 ignored issues
show
Bug Best Practice introduced by
The property CampaignSource does not exist on Dynamic\ShortURL\Model\ShortURL. Since you implemented __get, consider adding a @property annotation.
Loading history...
133
            $result->addError('A Campaign Source is required before you can save');
134
        }
135
136 1
        return $result;
137
    }
138
139
    /**
140
     * @return mixed
141
     */
142 1
    public function getToken()
143
    {
144 1
        return Config::inst()->get(ShortURL::class, 'bitly_token');
145
    }
146
147
    /**
148
     * @return string
149
     */
150 2
    public function getLongURL()
151
    {
152
        $vars = [
153 2
            'utm_source' => $this->CampaignSource,
0 ignored issues
show
Bug Best Practice introduced by
The property CampaignSource does not exist on Dynamic\ShortURL\Model\ShortURL. Since you implemented __get, consider adding a @property annotation.
Loading history...
154 2
            'utm_medium' => $this->CampaignMedium,
0 ignored issues
show
Bug Best Practice introduced by
The property CampaignMedium does not exist on Dynamic\ShortURL\Model\ShortURL. Since you implemented __get, consider adding a @property annotation.
Loading history...
155 2
            'utm_campaign' => $this->CampaignName,
0 ignored issues
show
Bug Best Practice introduced by
The property CampaignName does not exist on Dynamic\ShortURL\Model\ShortURL. Since you implemented __get, consider adding a @property annotation.
Loading history...
156 2
            'utm_term' => $this->CampaignTerm,
0 ignored issues
show
Bug Best Practice introduced by
The property CampaignTerm does not exist on Dynamic\ShortURL\Model\ShortURL. Since you implemented __get, consider adding a @property annotation.
Loading history...
157 2
            'utm_content' => $this->CampaignTerm,
158
        ];
159 2
        $LongURL = $this->URL . '?' . http_build_query($vars);
0 ignored issues
show
Bug Best Practice introduced by
The property URL does not exist on Dynamic\ShortURL\Model\ShortURL. Since you implemented __get, consider adding a @property annotation.
Loading history...
160 2
        return $LongURL;
161
    }
162
163
    /**
164
     *
165
     */
166 1
    public function onBeforeWrite()
167
    {
168 1
        parent::onBeforeWrite();
169
170 1
        if ($token = $this->getToken()) {
171
            $bitly = new Client($token);
172
173
            $response = $bitly->Shorten([
174
                'longUrl' => $this->getLongURL(),
175
                'format' => 'txt'
176
            ]);
177
178
            $this->ShortURL = $response['url'];
0 ignored issues
show
Bug Best Practice introduced by
The property ShortURL does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
179
        }
180
    }
181
}
182