Completed
Pull Request — master (#608)
by
unknown
12:02
created

getRouteGroupDescriptionAndTitle()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 8.223
c 0
b 0
f 0
cc 7
nc 9
nop 2
1
<?php
2
3
namespace Mpociot\ApiDoc\Extracting\Strategies\Metadata;
4
5
use ReflectionClass;
6
use ReflectionMethod;
7
use Illuminate\Routing\Route;
8
use Mpociot\Reflection\DocBlock;
9
use Mpociot\Reflection\DocBlock\Tag;
10
use Mpociot\ApiDoc\Extracting\RouteDocBlocker;
11
use Mpociot\ApiDoc\Extracting\Strategies\Strategy;
12
13
class GetFromDocBlocks extends Strategy
14
{
15
    public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $routeRules, array $context = [])
16
    {
17
        $docBlocks = RouteDocBlocker::getDocBlocksFromRoute($route);
18
        /** @var DocBlock $methodDocBlock */
19
        $methodDocBlock = $docBlocks['method'];
20
21
        list($routeGroupName, $routeGroupDescription, $routeTitle) = $this->getRouteGroupDescriptionAndTitle($methodDocBlock, $docBlocks['class']);
22
23
        return [
24
                'groupName' => $routeGroupName,
25
                'groupDescription' => $routeGroupDescription,
26
                'title' => $routeTitle ?: $methodDocBlock->getShortDescription(),
27
                'description' => $methodDocBlock->getLongDescription()->getContents(),
28
                'authenticated' => $this->getAuthStatusFromDocBlock($methodDocBlock->getTags()),
29
        ];
30
    }
31
32
    /**
33
     * @param array $tags Tags in the method doc block
34
     *
35
     * @return bool
36
     */
37
    protected function getAuthStatusFromDocBlock(array $tags)
38
    {
39
        $authTag = collect($tags)
40
            ->first(function ($tag) {
41
                return $tag instanceof Tag && strtolower($tag->getName()) === 'authenticated';
42
            });
43
44
        return (bool) $authTag;
45
    }
46
47
    /**
48
     * @param DocBlock $methodDocBlock
49
     * @param DocBlock $controllerDocBlock
50
     *
51
     * @return array The route group name, the group description, ad the route title
52
     */
53
    protected function getRouteGroupDescriptionAndTitle(DocBlock $methodDocBlock, DocBlock $controllerDocBlock)
54
    {
55
        // @group tag on the method overrides that on the controller
56
        if (! empty($methodDocBlock->getTags())) {
57
            foreach ($methodDocBlock->getTags() as $tag) {
58
                if ($tag->getName() === 'group') {
59
                    $routeGroupParts = explode("\n", trim($tag->getContent()));
60
                    $routeGroupName = array_shift($routeGroupParts);
61
                    $routeGroupDescription = trim(implode("\n", $routeGroupParts));
62
63
                    // If the route has no title (the methodDocBlock's "short description"),
64
                    // we'll assume the routeGroupDescription is actually the title
65
                    // Something like this:
66
                    // /**
67
                    //   * Fetch cars. <-- This is route title.
68
                    //   * @group Cars <-- This is group name.
69
                    //   * APIs for cars. <-- This is group description (not required).
70
                    //   **/
71
                    // VS
72
                    // /**
73
                    //   * @group Cars <-- This is group name.
74
                    //   * Fetch cars. <-- This is route title, NOT group description.
75
                    //   **/
76
77
                    // BTW, this is a spaghetti way of doing this.
78
                    // It shall be refactored soon. Deus vult!💪
79
                    if (empty($methodDocBlock->getShortDescription())) {
80
                        return [$routeGroupName, '', $routeGroupDescription];
81
                    }
82
83
                    return [$routeGroupName, $routeGroupDescription, $methodDocBlock->getShortDescription()];
84
                }
85
            }
86
        }
87
88
        foreach ($controllerDocBlock->getTags() as $tag) {
89
            if ($tag->getName() === 'group') {
90
                $routeGroupParts = explode("\n", trim($tag->getContent()));
91
                $routeGroupName = array_shift($routeGroupParts);
92
                $routeGroupDescription = implode("\n", $routeGroupParts);
93
94
                return [$routeGroupName, $routeGroupDescription, $methodDocBlock->getShortDescription()];
95
            }
96
        }
97
98
        return [$this->config->get('default_group'), '', $methodDocBlock->getShortDescription()];
99
    }
100
}
101