These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | class ShortURL extends DataObject |
||
4 | { |
||
5 | /** |
||
6 | * @var string |
||
7 | */ |
||
8 | private static $singular_name = 'Short URL'; |
||
9 | |||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private static $plural_name = 'Short URLs'; |
||
14 | |||
15 | private $bitly_token; |
||
0 ignored issues
–
show
|
|||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private static $db = [ |
||
21 | 'Title' => 'Varchar(255)', |
||
22 | 'URL' => 'Varchar(255)', |
||
23 | 'CampaignSource' => 'Varchar(255)', |
||
24 | 'CampaignMedium' => 'Varchar(255)', |
||
25 | 'CampaignName' => 'Varchar(255)', |
||
26 | 'CampaignTerm' => 'Varchar(255)', |
||
27 | 'CampaignContent' => 'Varchar(255)', |
||
28 | 'ShortURL' => 'Varchar(255)', |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private static $summary_fields = [ |
||
35 | 'Title', |
||
36 | 'URL', |
||
37 | 'ShortURL', |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private static $searchable_fields = [ |
||
44 | 'Title', |
||
45 | 'URL', |
||
46 | 'CampaignSource', |
||
47 | 'CampaignMedium', |
||
48 | 'CampaignName', |
||
49 | 'CampaignTerm', |
||
50 | 'CampaignContent', |
||
51 | 'ShortURL', |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * @return FieldList |
||
56 | */ |
||
57 | public function getCMSFields() |
||
58 | { |
||
59 | $fields = parent::getCMSFields(); |
||
60 | |||
61 | $fields->dataFieldByName('URL') |
||
62 | ->setDescription('URL to shorten and tag'); |
||
63 | |||
64 | $fields->dataFieldByName('CampaignSource') |
||
65 | ->setDescription('Use to identify a search engine, newsletter name, or other source. Example: google'); |
||
66 | |||
67 | $fields->dataFieldByName('CampaignMedium') |
||
68 | ->setDescription('Use utm_medium to identify a medium such as email or cost-per- click. Example: cpc'); |
||
69 | |||
70 | $fields->dataFieldByName('CampaignName') |
||
71 | ->setDescription('Used for keyword analysis. Use utm_campaign to identify a specific product promotion or strategic campaign. Example: utm_campaign=spring_sale'); |
||
72 | |||
73 | $fields->dataFieldByName('CampaignTerm') |
||
74 | ->setDescription('Used for paid search. Use utm_term to note the keywords for this ad. Example: running+shoes'); |
||
75 | |||
76 | $fields->dataFieldByName('CampaignContent') |
||
77 | ->setDescription('Used for A/B testing and content-targeted ads. Use utm_content to differentiate ads or links that point to the same URL. Examples: logolink or textlink'); |
||
78 | |||
79 | $fields->addFieldToTab('Root.Main', ReadonlyField::create('LongURL', 'Long URL', $this->getLongURL())); |
||
80 | |||
81 | $short = $fields->dataFieldByName('ShortURL'); |
||
82 | $short = $short->performReadonlyTransformation(); |
||
83 | $fields->addFieldToTab('Root.Main', $short); |
||
84 | |||
85 | return $fields; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return ValidationResult |
||
90 | */ |
||
91 | public function validate() |
||
92 | { |
||
93 | $result = parent::validate(); |
||
94 | |||
95 | if (!$this->Title) { |
||
96 | $result->error('A Title is required before you can save'); |
||
97 | } |
||
98 | |||
99 | if (!$this->URL) { |
||
100 | $result->error('A URL is required before you can save'); |
||
101 | } |
||
102 | |||
103 | if (!$this->CampaignSource) { |
||
104 | $result->error('A Campaign Source is required before you can save'); |
||
105 | } |
||
106 | |||
107 | return $result; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getLongURL() |
||
114 | { |
||
115 | $vars = [ |
||
116 | 'utm_source' => $this->CampaignSource, |
||
117 | 'utm_medium' => $this->CampaignMedium, |
||
118 | 'utm_campaign' => $this->CampaignName, |
||
119 | 'utm_term' => $this->CampaignTerm, |
||
120 | 'utm_content' => $this->CampaignTerm, |
||
121 | ]; |
||
122 | $LongURL = $this->URL . '?' . http_build_query($vars); |
||
123 | return $LongURL; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * |
||
128 | */ |
||
129 | public function onBeforeWrite() |
||
130 | { |
||
131 | parent::onBeforeWrite(); |
||
132 | |||
133 | if ($token = Config::inst()->get('ShortURL', 'bitly_token')) { |
||
134 | $bitly = new \Hpatoio\Bitly\Client($token); |
||
135 | |||
136 | $response = $bitly->Shorten([ |
||
137 | 'longUrl' => $this->getLongURL(), |
||
138 | 'format' => 'txt' |
||
139 | ]); |
||
140 | |||
141 | $this->ShortURL = $response['url']; |
||
142 | } |
||
143 | } |
||
144 | } |
This check marks private properties in classes that are never used. Those properties can be removed.