Test Failed
Push — main ( ea931b...8f4107 )
by Bingo
06:03
created

ResourceUtil::loadResourceContent()   B

Complexity

Conditions 7
Paths 28

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 36
rs 8.6026
cc 7
nc 28
nop 2
1
<?php
2
3
namespace Jabe\Engine\Impl\Util;
4
5
use Jabe\Engine\Exception\NotFoundException;
6
use Jabe\Engine\Impl\ProcessEngineLogger;
7
use Jabe\Engine\Impl\Persistence\Entity\{
8
    DeploymentEntity,
9
    ResourceEntity
10
};
11
12
class ResourceUtil
13
{
14
    //private static final EngineUtilLogger LOG = ProcessEngineLogger.UTIL_LOGGER;
15
16
    /**
17
     * Parse a camunda:resource attribute and loads the resource depending on the url scheme.
18
     * Supported URL schemes are <code>classpath://</code> and <code>deployment://</code>.
19
     * If the scheme is omitted <code>classpath://</code> is assumed.
20
     *
21
     * @param resourcePath the path to the resource to load
22
     * @param deployment the deployment to load resources from
23
     * @return the resource content as {@link String}
24
     */
25
    public static function loadResourceContent(string $resourcePath, DeploymentEntity $deployment): string
26
    {
27
        $pathSplit = explode("://", $resourcePath);
28
29
        $resourceType = null;
30
        if (count($pathSplit) == 1) {
31
            $resourceType = "classpath";
32
        } else {
33
            $resourceType = $pathSplit[0];
34
        }
35
36
        $resourceLocation = $pathSplit[count($pathSplit) - 1];
37
38
        $resourceBytes = null;
39
40
        if ($resourceType == "classpath") {
41
            $resourceAsStream = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $resourceAsStream is dead and can be removed.
Loading history...
42
            try {
43
                $resourceAsStream = ReflectUtil::getResourceAsStream($resourceLocation);
44
                if ($resourceAsStream !== null) {
45
                    $resourceBytes = IoUtil::readInputStream($resourceAsStream, $resourcePath);
46
                }
47
            } finally {
48
                IoUtil::closeSilently($resourceAsStream);
49
            }
50
        } elseif ($resourceType == "deployment") {
51
            $resourceEntity = $deployment->getResource($resourceLocation);
52
            if ($resourceEntity !== null) {
53
                $resourceBytes = $resourceEntity->getBytes();
54
            }
55
        }
56
57
        if ($resourceBytes !== null) {
58
            return $resourceBytes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $resourceBytes returns the type string which is incompatible with the documented return type Jabe\Engine\Impl\Util\the.
Loading history...
59
        } else {
60
            throw new \Exception(sprintf("cannotFindResource %s", $resourcePath));
61
        }
62
    }
63
}
64