Completed
Push — master ( b8da20...856b73 )
by
unknown
02:45
created

RemoteFileModalExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 12
dl 0
loc 123
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getOwner() 0 6 1
A getRequest() 0 4 1
A getFormSchema() 0 4 1
A remoteCreateForm() 0 9 1
A remoteEditForm() 0 12 1
A remoteEditFormSchema() 0 22 3
A getSchemaResponse() 0 15 2
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Extensions;
4
5
use Embed\Exceptions\InvalidUrlException;
6
use SilverStripe\Admin\LeftAndMain;
7
use SilverStripe\Admin\ModalController;
8
use SilverStripe\AssetAdmin\Forms\RemoteFileFormFactory;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Control\HTTPResponse;
11
use SilverStripe\Core\Convert;
12
use SilverStripe\Core\Extension;
13
use SilverStripe\Core\Injector\Injector;
14
use SilverStripe\Forms\FieldList;
15
use SilverStripe\Forms\Form;
16
use SilverStripe\Forms\Schema\FormSchema;
17
use SilverStripe\ORM\ValidationResult;
18
19
/**
20
 * Decorates ModalController with an insert-oembed modal
21
 * @see ModalController
22
 */
23
class RemoteFileModalExtension extends Extension
24
{
25
    private static $allowed_actions = 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 $allowed_actions 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...
26
        'remoteCreateForm',
27
        'remoteEditForm',
28
        'remoteEditFormSchema',
29
    );
30
31
    /**
32
     * @return ModalController
33
     */
34
    public function getOwner()
35
    {
36
        /** @var ModalController $owner */
37
        $owner = $this->owner;
38
        return $owner;
39
    }
40
41
    /**
42
     * @return HTTPRequest
43
     */
44
    protected function getRequest()
45
    {
46
        return $this->getOwner()->getController()->getRequest();
47
    }
48
49
    /**
50
     * @return FormSchema
51
     */
52
    protected function getFormSchema()
53
    {
54
        return FormSchema::singleton();
55
    }
56
57
    /**
58
     * Form for creating a new OEmbed object in the WYSIWYG, used by the InsertEmbedModal component
59
     *
60
     * @return Form
61
     */
62
    public function remoteCreateForm()
63
    {
64
        return Injector::inst()->get(RemoteFileFormFactory::class)
65
            ->getForm(
66
                $this->getOwner(),
67
                'remoteCreateForm',
68
                ['type' => 'create']
69
            );
70
    }
71
72
    /**
73
     * Form for editing a OEmbed object in the WYSIWYG, used by the InsertEmbedModal component
74
     *
75
     * @return Form
76
     */
77
    public function remoteEditForm()
78
    {
79
        $url = $this->getRequest()->requestVar('embedurl');
80
        $form = null;
0 ignored issues
show
Unused Code introduced by
$form is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
        $form = Injector::inst()->get(RemoteFileFormFactory::class)
82
            ->getForm(
83
                $this->getOwner(),
84
                'remoteEditForm',
85
                ['type' => 'edit', 'url' => $url]
86
            );
87
        return $form;
88
    }
89
90
    /**
91
     * Capture the schema handling process, as there is validation done to the URL provided before form is generated
92
     *
93
     * @param HTTPRequest $request
94
     * @return HTTPResponse
95
     */
96
    public function remoteEditFormSchema(HTTPRequest $request)
97
    {
98
        $schemaID = $request->getURL();
99
        try {
100
            $form = $this->remoteEditForm();
101
            return $this->getSchemaResponse($schemaID, $form);
102
        } catch (InvalidUrlException $exception) {
103
            $errors = ValidationResult::create()
104
                ->addError($exception->getMessage());
105
            // @todo - Don't create dummy form (pass $form = null)
106
            $form = Form::create(null, 'Form', FieldList::create(), FieldList::create());
107
            $code = $exception->getCode();
108
109
            if ($code < 300) {
110
                $code = 500;
111
            }
112
113
            return $this
114
                ->getSchemaResponse($schemaID, $form, $errors)
115
                ->setStatusCode($code);
116
        }
117
    }
118
119
    /**
120
     * Generate schema for the given form based on the X-Formschema-Request header value
121
     *
122
     * @todo de-dupe this logic with LeftAndMain::getSchemaResponse()
123
     *
124
     * @param string $schemaID ID for this schema. Required.
125
     * @param Form $form Required for 'state' or 'schema' response
126
     * @param ValidationResult $errors Required for 'error' response
127
     * @param array $extraData Any extra data to be merged with the schema response
128
     * @return HTTPResponse
129
     */
130
    protected function getSchemaResponse($schemaID, $form = null, ValidationResult $errors = null, $extraData = [])
131
    {
132
        $parts = $this->getRequest()->getHeader(LeftAndMain::SCHEMA_HEADER);
133
        $data = $this
134
            ->getFormSchema()
135
            ->getMultipartSchema($parts, $schemaID, $form, $errors);
136
137
        if ($extraData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $extraData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
138
            $data = array_merge($data, $extraData);
139
        }
140
141
        $response = new HTTPResponse(Convert::raw2json($data));
142
        $response->addHeader('Content-Type', 'application/json');
143
        return $response;
144
    }
145
}
146