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

code/ShortURL.php (11 issues)

Upgrade to new PHP Analysis Engine

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
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...
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) {
0 ignored issues
show
The property Title does not exist on object<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...
96
            $result->error('A Title is required before you can save');
97
        }
98
99
        if (!$this->URL) {
0 ignored issues
show
The property URL does not exist on object<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...
100
            $result->error('A URL is required before you can save');
101
        }
102
103
        if (!$this->CampaignSource) {
0 ignored issues
show
The property CampaignSource does not exist on object<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...
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,
0 ignored issues
show
The property CampaignSource does not exist on object<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...
117
            'utm_medium' => $this->CampaignMedium,
0 ignored issues
show
The property CampaignMedium does not exist on object<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...
118
            'utm_campaign' => $this->CampaignName,
0 ignored issues
show
The property CampaignName does not exist on object<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...
119
            'utm_term' => $this->CampaignTerm,
0 ignored issues
show
The property CampaignTerm does not exist on object<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...
120
            'utm_content' => $this->CampaignTerm,
0 ignored issues
show
The property CampaignTerm does not exist on object<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...
121
        ];
122
        $LongURL = $this->URL . '?' . http_build_query($vars);
0 ignored issues
show
The property URL does not exist on object<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...
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'];
0 ignored issues
show
The property ShortURL does not exist on object<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...
142
        }
143
    }
144
}