WebPortfolioItem::Link()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.2
cc 4
eloc 9
nc 4
nop 0
1
<?php
2
3
 /**
4
 * @author Nicolaas [at] sunnysideup.co.nz
5
 *
6
 *
7
 *
8
 *
9
 *
10
 *
11
 */
12
13
class WebPortfolioItem extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
{
15
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db 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
        "WebAddress" => "Varchar(255)",
17
        "NoLongerActive" => "Boolean",
18
        "NotPubliclyAvailable" => "Boolean",
19
        "Favourites" => "Boolean",
20
        "Notes" => "Varchar(255)",
21
        "Client" => "Varchar(255)",
22
        "Design" => "Varchar(255)",
23
        "CodingFrontEnd" => "Varchar(255)",
24
        "CodingBackEnd" => "Varchar(255)",
25
        "Copy" => "Varchar(255)",
26
        "Photography" => "Varchar(255)",
27
        "ScreenshotTaken" => "Date",
28
        "StartDate" => "Date",
29
        "EndDate" => "Date"
30
    );
31
32
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one 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...
33
        "Agent" => "WebPortfolioAgent",
34
        "Screenshot" => "Image",
35
    );
36
37
    private static $many_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $many_many 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...
38
        "WhatWeDid" => "WebPortfolioWhatWeDidDescriptor",
39
    );
40
41
    private static $belongs_many_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $belongs_many_many 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...
42
        "WhatWeDid" => "WebPortfolioWhatWeDidDescriptor",
43
    );
44
45
    private static $defaults = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $defaults 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...
46
        "WebAddress" => "http",
47
        "NoLongerActive" => false,
48
        "Favourites" => false
49
    );
50
51
    private static $default_sort = "Favourites DESC, Created DESC";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $default_sort 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...
52
53
    private static $searchable_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $searchable_fields 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...
54
        "WebAddress",
55
        "Client",
56
        "NoLongerActive",
57
        "NotPubliclyAvailable",
58
        "Favourites",
59
        "Agent.Name"
60
    );
61
62
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields 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...
63
        "WebAddress",
64
        "Client",
65
        "Thumbnail"
66
    );
67
68
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $casting 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...
69
        "Title" => "Varchar",
70
        "Thumbnail" => "HTMLText",
71
        "HeadLine" => "Varchar"
72
    );
73
74
    private static $singular_name = "Item";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $singular_name 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...
75
76
    private static $plural_name = "Items";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $plural_name 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...
77
78
79
    public function getCMSFields()
80
    {
81
        $fields = parent::getCMSFields();
82
        $dos = WebPortfolioWhatWeDidDescriptor::get();
83
        if ($dos->count() && $this->ID) {
84
            $dosArray = $dos->map()->toArray();
85
            $fields->addFieldsToTab(
86
                "Root.WhatWeDid",
87
                array(
88
                    new CheckboxSetField("WhatWeDid", "Select work done", $dosArray),
89
                    new TextField("AddWhatWeDid", "Add a job")
90
                )
91
            );
92
        }
93
        if (class_exists("DataObjectOneFieldUpdateController")) {
94
            $editableFields = array_keys($this->Config()->get("db"));
95
            foreach ($editableFields as $editableField) {
96
                $link = DataObjectOneFieldUpdateController::popup_link(
97
                    $ClassName = $this->ClassName,
98
                    $FieldName = $editableField,
99
                    $where = '',
100
                    $sort = '"'.$editableField.'" DESC',
101
                    $linkText = $editableField
102
                );
103
                $fields->addFieldToTab("Root.QuickEdits", new LiteralField("QuickEdit".$editableField, "<h2>".$link."</h2>"));
104
            }
105
        }
106
        return $fields;
107
    }
108
109
    public function getHeadLine()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
110
    {
111
        $searchArray = array(
112
            "https://www.",
113
            "http://www.",
114
            "https://",
115
            "http://",
116
            "."
117
        );
118
        $replaceArray = array(
119
            "",
120
            "",
121
            "",
122
            "",
123
            " . "
124
        );
125
        return str_replace($searchArray, $replaceArray, $this->WebAddress);
0 ignored issues
show
Documentation introduced by
The property WebAddress does not exist on object<WebPortfolioItem>. 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...
126
    }
127
128
    protected $newWhatWeDid = null;
129
130
    public function onAfterWrite()
0 ignored issues
show
Coding Style introduced by
onAfterWrite uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
131
    {
132
        parent::onAfterWrite();
133
        if ($this->newWhatWeDid) {
134
            $this->newWhatWeDid->WebPortfolioItem()->add($this);
135
            $this->WhatWeDid()->add($this->newWhatWeDid);
0 ignored issues
show
Documentation Bug introduced by
The method WhatWeDid does not exist on object<WebPortfolioItem>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
136
        }
137
        if (isset($_REQUEST["AddWhatWeDid"])) {
138
            unset($_REQUEST["AddWhatWeDid"]);
139
        }
140
        $this->newWhatWeDid = null;
141
    }
142
143
    public function onBeforeWrite()
0 ignored issues
show
Coding Style introduced by
onBeforeWrite uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
144
    {
145
        parent::onBeforeWrite();
146
        if (isset($_REQUEST["AddWhatWeDid"])) {
147
            $name = Convert::raw2sql($_REQUEST["AddWhatWeDid"]);
148
            if ($name) {
149
                $this->newWhatWeDid = WebPortfolioWhatWeDidDescriptor::get()
150
                    ->filter(array("Name" => $name))->first();
151
                if (!$this->newWhatWeDid) {
152
                    $this->newWhatWeDid = new WebPortfolioWhatWeDidDescriptor();
153
                    $this->newWhatWeDid->Name = $name;
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<WebPortfolioWhatWeDidDescriptor>. 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...
154
                    $this->newWhatWeDid->write();
155
                    //TO DO - does not work!!!
156
                }
157
            }
158
        }
159
    }
160
161
    public function Title()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
162
    {
163
        return $this->getTitle();
164
    }
165
    public function getTitle()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
166
    {
167
        return $this->WebAddress;
0 ignored issues
show
Documentation introduced by
The property WebAddress does not exist on object<WebPortfolioItem>. 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...
168
    }
169
170
171
    public function Link()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
172
    {
173
        $link = "";
174
        $page = WebPortfolioPage::get()->first();
175
        if ($page) {
176
            $link = $page->Link("show/".$this->ID."/");
177
        } elseif ($this->ScreenshotID) {
0 ignored issues
show
Documentation introduced by
The property ScreenshotID does not exist on object<WebPortfolioItem>. 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...
178
            if ($screenshot = $this->Screenshot()) {
0 ignored issues
show
Documentation Bug introduced by
The method Screenshot does not exist on object<WebPortfolioItem>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
179
                $link = $screenshot->Link();
180
            }
181
        }
182
        return $link;
183
    }
184
185
    public function getThumbnail()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
186
    {
187
        return $this->Thumbnail();
188
    }
189
    public function Thumbnail()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
190
    {
191
        if ($this->ScreenshotID) {
0 ignored issues
show
Documentation introduced by
The property ScreenshotID does not exist on object<WebPortfolioItem>. 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...
192
            if ($image = $this->Screenshot()) {
0 ignored issues
show
Documentation Bug introduced by
The method Screenshot does not exist on object<WebPortfolioItem>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
193
                if ($image->exists()) {
194
                    return $image->CroppedImage(100, 100);
195
                }
196
            }
197
            return "image can not be found";
198
        }
199
        return "no image";
200
    }
201
}
202