Issues (78)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/jsonApi/ResourceDocumentFactory.php (1 issue)

Labels
Severity

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
declare(strict_types=1);
3
4
namespace hiapi\jsonApi;
5
6
use Psr\Container\ContainerInterface;
7
use RuntimeException;
8
use WoohooLabs\Yin\JsonApi\Schema\Resource\ResourceInterface;
9
use WoohooLabs\Yin\JsonApi\Schema\Document\ResourceDocumentInterface;
10
11
/**
12
 * Creates JsonApi resource document for given content.
13
 *
14
 * @author Andrii Vasyliev <[email protected]>
15
 */
16
class ResourceDocumentFactory implements ResourceDocumentFactoryInterface
17
{
18
    private ContainerInterface $container;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
19
20
    private array $resourceMap;
21
22
    private array $collections = [];
23
24
    private array $resources = [];
25
26
    public function __construct(array $resourceMap, ContainerInterface $container)
27
    {
28
        $this->resourceMap = $resourceMap;
29
        $this->container = $container;
30
    }
31
32
    /** {@inheritDoc} */
33
    public function getResourceDocumentFor($content): ResourceDocumentInterface
34
    {
35
        if (is_array($content)) {
36
            return $this->getCollectionDocument($content);
37
        }
38
39
        return $this->getSingleDocument($content);
40
    }
41
42
    /** {@inheritDoc} */
43
    public function getResourceByClassName(string $entityClassName): ResourceInterface
44
    {
45
        if (empty($this->resources[$entityClassName])) {
46
            $this->resources[$entityClassName] = $this->container->get(
47
                $this->findResourceClass($entityClassName)
48
            );
49
        }
50
51
        return $this->resources[$entityClassName];
52
    }
53
54
    /** {@inheritDoc} */
55
    public function getResourceFor(object $entity): ResourceInterface
56
    {
57
        $class = get_class($entity);
58
59
        return $this->getResourceByClassName($class);
60
    }
61
62
    private function findResourceClass(string $class): string
63
    {
64
        if (! empty($this->resourceMap[$class])) {
65
            return $this->resourceMap[$class];
66
        }
67
68
        $res = $this->findResourceClassByAttribution($class);
69
70
        return $res ?? $this->findResourceClassByAncestor($class);
71
72
    }
73
74
    private function findResourceClassByAttribution($class): ?string
75
    {
76
        $result = substr($class, 0, -11) . 'Resource';
77
78
        return class_exists($result) ? $result : null;
79
    }
80
81
    private function findResourceClassByAncestor($class): string
82
    {
83
        foreach ($this->resourceMap as $ancestor => $result) {
84
            if (is_subclass_of($class, $ancestor)) {
85
                return $result;
86
            }
87
        }
88
89
        throw new RuntimeException("JsonApi resource not defined for '$class'");
90
    }
91
92
    private function getCollectionDocument(array $rows): ResourceDocumentInterface
93
    {
94
        if (empty($rows)) {
95
            return new EmptyCollectionDocument();
96
        }
97
        $class = get_class(reset($rows));
98
        if (empty($this->collections[$class])) {
99
            $this->collections[$class] = $this->findCollection($class);
100
        }
101
102
        return $this->collections[$class];
103
    }
104
105
    private function getSingleDocument(object $entity): ResourceDocumentInterface
106
    {
107
        $singleDocumentClass = $this->resource2singleDocument(
108
            $this->findResourceClass(get_class($entity))
109
        );
110
111
        return $this->container->get($singleDocumentClass);
112
    }
113
114
    private function findCollection(string $class): ResourceDocumentInterface
115
    {
116
        $class = $this->resource2collection($this->findResourceClass($class));
117
118
        return $this->container->get($class);
119
    }
120
121
    private function resource2collection(string $class): string
122
    {
123
        // XXX TMP quick and dirty TODO improve later
124
        return substr($class, 0, -8) . 'sCollectionDocument';
125
    }
126
127
    private function resource2singleDocument(string $class): string
128
    {
129
        // XXX TMP quick and dirty TODO improve later
130
        return substr($class, 0, -8) . 'Document';
131
    }
132
}
133