Completed
Push — master ( 364b5f...db455e )
by
unknown
04:01
created

code/api/EcommerceRestfulServer.php (3 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
 * extends the standard RestfulServer to provide better access to extended classes.
4
 *
5
 * see: http://api.silverstripe.org/2.4/sapphire/api/RestfulServer.html
6
 *
7
 * You can show JSON by hacking: RestfulServer::getDataFormatter
8
 * NOTE: JSON IS NOT AVAILABLE YET WITHIN RESTFUL SERVER
9
 *
10
 * @todo:
11
 * - fix http://site/api/ecommerce/v1/Order/123/BillingAddress.xml
12
 * - fix http://site/api/ecommerce/v1/Order/123/ShippingAddress.xml
13
 * - fix http://site/api/ecommerce/v1/Order/123/Member.xml
14
 *
15
 * <b>Test Post</b>
16
 * <code>
17
 * $baseURL = Director::absoluteBaseURL();
18
 * 	// 1) My Personal Data
19
 * 	$className = 'EcommerceClassWithEditableFields';
20
 * 	$fields = array(
21
 * 		'MyField' => 1
22
 * 	);
23
 * 	// 2) The Query
24
 * 	$url = "{$baseURL}/api/ecommerce/v1/{$className}.xml";
25
 * 	$body = $fields;
26
 * 	$c = curl_init($url);
27
 * 	curl_setopt($c, CURLOPT_POST, true);
28
 * 	curl_setopt($c, CURLOPT_POSTFIELDS, $body);
29
 * 	curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
30
 * 	$page = curl_exec($c);
31
 * 	curl_close($c);
32
33
 * 	// 3) The XML Result
34
35
 * 	return $page;
36
 * </code>
37
 *
38
 *
39
 * @authors: Romain [at] Sunny Side Up .co.nz
40
 * @package: ecommerce
41
 * @sub-package: api
42
 * @inspiration: Silverstripe Ltd, Jeremy
43
 **/
44
class EcommerceRestfulServer extends RestfulServer
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...
45
{
46
    public function index()
0 ignored issues
show
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...
47
    {
48
        XMLDataFormatter::$api_base = 'api/ecommerce/v1/';
0 ignored issues
show
The property api_base cannot be accessed from this context as it is declared private in class XMLDataFormatter.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
49
        if (!isset($this->urlParams['ClassName'])) {
50
            return $this->notFound();
51
        }
52
        $className = $this->urlParams['ClassName'];
53
        $id = (isset($this->urlParams['ID'])) ? $this->urlParams['ID'] : null;
54
        $relation = (isset($this->urlParams['Relation'])) ? $this->urlParams['Relation'] : null;
55
56
        // Check input formats
57
        if (!class_exists($className)) {
58
            return $this->notFound();
59
        }
60
        if ($id && !is_numeric($id)) {
61
            return $this->notFound();
62
        }
63
        if ($relation && !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $relation)) {
64
            return $this->notFound();
65
        }
66
67
        // fix
68
        if ($id) {
69
            $obj = $className::get()->byID($id);
70
            if ($obj) {
71
                $className = $this->urlParams['ClassName'] = $obj->ClassName;
72
            } else {
73
                return $this->notFound();
74
            }
75
        }
76
77
        // if api access is disabled, don't proceed
78
        $apiAccess = singleton($className)->stat('api_access');
79
        if (!$apiAccess) {
80
            return $this->permissionFailure();
81
        }
82
83
        // authenticate through HTTP BasicAuth
84
        $this->member = $this->authenticate();
85
86
        // handle different HTTP verbs
87
        if ($this->request->isGET() || $this->request->isHEAD()) {
88
            return $this->getHandler($className, $id, $relation);
89
        }
90
        if ($this->request->isPOST()) {
91
            return $this->postHandler($className, $id, $relation);
92
        }
93
        if ($this->request->isPUT()) {
94
            return $this->putHandler($className, $id, $relation);
95
        }
96
        if ($this->request->isDELETE()) {
97
            return $this->deleteHandler($className, $id, $relation);
98
        }
99
100
        // if no HTTP verb matches, return error
101
        return $this->methodNotAllowed();
102
    }
103
}
104