Completed
Push — master ( 7b4143...7f762c )
by Jason
10s
created

ShortURL::getLongURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\ShortURL;
4
5
use \Hpatoio\Bitly\Client;
6
7
class ShortURL extends \DataObject
8
{
9
    /**
10
     * @var string
11
     */
12
    private static $singular_name = 'Short URL';
13
14
    /**
15
     * @var string
16
     */
17
    private static $plural_name = 'Short URLs';
18
19
    /**
20
     * @var
21
     */
22
    private $bitly_token;
0 ignored issues
show
Unused Code introduced by
The property $bitly_token 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...
23
24
    /**
25
     * @var array
26
     */
27
    private static $db = [
28
        'Title' => 'Varchar(255)',
29
        'URL' => 'Varchar(255)',
30
        'CampaignSource' => 'Varchar(255)',
31
        'CampaignMedium' => 'Varchar(255)',
32
        'CampaignName' => 'Varchar(255)',
33
        'CampaignTerm' => 'Varchar(255)',
34
        'CampaignContent' => 'Varchar(255)',
35
        'ShortURL' => 'Varchar(255)',
36
    ];
37
38
    /**
39
     * @var array
40
     */
41
    private static $summary_fields = [
42
        'Title',
43
        'URL',
44
        'ShortURL',
45
    ];
46
47
    /**
48
     * @var array
49
     */
50
    private static $searchable_fields = [
51
        'Title',
52
        'URL',
53
        'CampaignSource',
54
        'CampaignMedium',
55
        'CampaignName',
56
        'CampaignTerm',
57
        'CampaignContent',
58
        'ShortURL',
59
    ];
60
61
    /**
62
     * @return FieldList
63
     */
64
    public function getCMSFields()
65
    {
66
        $fields = parent::getCMSFields();
67
68
        $fields->dataFieldByName('URL')
69
            ->setDescription('URL to shorten and tag');
70
71
        $fields->dataFieldByName('CampaignSource')
72
            ->setDescription('Use to identify a search engine, newsletter name, or other source. Example: google');
73
74
        $fields->dataFieldByName('CampaignMedium')
75
            ->setDescription('Use utm_medium to identify a medium such as email or cost-per- click. Example: cpc');
76
77
        $fields->dataFieldByName('CampaignName')
78
            ->setDescription('Used for keyword analysis. Use utm_campaign to identify a specific product promotion or strategic campaign. Example: utm_campaign=spring_sale');
79
80
        $fields->dataFieldByName('CampaignTerm')
81
            ->setDescription('Used for paid search. Use utm_term to note the keywords for this ad. Example: running+shoes');
82
83
        $fields->dataFieldByName('CampaignContent')
84
            ->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');
85
86
        $fields->addFieldToTab('Root.Main', \ReadonlyField::create('LongURL', 'Long URL', $this->getLongURL()));
87
88
        $short = $fields->dataFieldByName('ShortURL');
89
        $short = $short->performReadonlyTransformation();
90
        $fields->addFieldToTab('Root.Main', $short);
91
92
        return $fields;
93
    }
94
95
    /**
96
     * @return ValidationResult
97
     */
98
    public function validate()
99
    {
100
        $result = parent::validate();
101
102
        if (!$this->Title) {
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<Dynamic\ShortURL\ShortURL>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
            $result->error('A Title is required before you can save');
104
        }
105
106
        if (!$this->URL) {
0 ignored issues
show
Documentation introduced by
The property URL does not exist on object<Dynamic\ShortURL\ShortURL>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
107
            $result->error('A URL is required before you can save');
108
        }
109
110
        if (!$this->CampaignSource) {
0 ignored issues
show
Documentation introduced by
The property CampaignSource does not exist on object<Dynamic\ShortURL\ShortURL>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
111
            $result->error('A Campaign Source is required before you can save');
112
        }
113
114
        return $result;
115
    }
116
117
    /**
118
     * @return array|\scalar
119
     */
120
    public function getToken()
121
    {
122
        return \Config::inst()->get('Dynamic\ShortURL\ShortURL', 'bitly_token');
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getLongURL()
129
    {
130
        $vars = [
131
            'utm_source' => $this->CampaignSource,
0 ignored issues
show
Documentation introduced by
The property CampaignSource does not exist on object<Dynamic\ShortURL\ShortURL>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
132
            'utm_medium' => $this->CampaignMedium,
0 ignored issues
show
Documentation introduced by
The property CampaignMedium does not exist on object<Dynamic\ShortURL\ShortURL>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
133
            'utm_campaign' => $this->CampaignName,
0 ignored issues
show
Documentation introduced by
The property CampaignName does not exist on object<Dynamic\ShortURL\ShortURL>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
134
            'utm_term' => $this->CampaignTerm,
0 ignored issues
show
Documentation introduced by
The property CampaignTerm does not exist on object<Dynamic\ShortURL\ShortURL>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
135
            'utm_content' => $this->CampaignTerm,
0 ignored issues
show
Documentation introduced by
The property CampaignTerm does not exist on object<Dynamic\ShortURL\ShortURL>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
136
        ];
137
        $LongURL = $this->URL . '?' . http_build_query($vars);
0 ignored issues
show
Documentation introduced by
The property URL does not exist on object<Dynamic\ShortURL\ShortURL>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
138
        return $LongURL;
139
    }
140
141
    /**
142
     *
143
     */
144
    public function onBeforeWrite()
145
    {
146
        parent::onBeforeWrite();
147
148
        if ($token = $this->getToken()) {
149
            $bitly = new Client($token);
150
151
            $response = $bitly->Shorten([
152
                'longUrl' => $this->getLongURL(),
153
                'format' => 'txt'
154
            ]);
155
156
            $this->ShortURL = $response['url'];
0 ignored issues
show
Documentation introduced by
The property ShortURL does not exist on object<Dynamic\ShortURL\ShortURL>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
157
        }
158
    }
159
}