Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| DocumentationSidebarService | |
100.00% |
19 / 19 |
|
100.00% |
10 / 10 |
11 | |
100.00% |
1 / 1 |
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createSidebar | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getSidebar | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSortedSidebar | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addItem | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| withoutIndex | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| withoutHidden | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getSidebarItems | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createSidebarItemFromSlug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Hyde\Framework\Services; |
| 4 | |
| 5 | use Hyde\Framework\Concerns\HasDocumentationSidebarCategories; |
| 6 | use Hyde\Framework\Contracts\DocumentationSidebarServiceContract; |
| 7 | use Hyde\Framework\Models\DocumentationSidebar; |
| 8 | use Hyde\Framework\Models\DocumentationSidebarItem; |
| 9 | |
| 10 | /** |
| 11 | * Service class to create and manage the sidebar collection object. |
| 12 | * |
| 13 | * @see \Tests\Feature\Services\DocumentationSidebarServiceTest |
| 14 | */ |
| 15 | class DocumentationSidebarService implements DocumentationSidebarServiceContract |
| 16 | { |
| 17 | use HasDocumentationSidebarCategories; |
| 18 | |
| 19 | /** |
| 20 | * The sidebar object created and managed by the service instance. |
| 21 | */ |
| 22 | protected DocumentationSidebar $sidebar; |
| 23 | |
| 24 | /** |
| 25 | * Shorthand to create a new Sidebar service using default methods. |
| 26 | */ |
| 27 | public static function create(): static |
| 28 | { |
| 29 | return (new static)->createSidebar()->withoutIndex()->withoutHidden(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Shorthand to create a new Sidebar object using default methods. |
| 34 | */ |
| 35 | public static function get(): DocumentationSidebar |
| 36 | { |
| 37 | return static::create()->getSidebar()->sortItems()->getCollection(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Parse the _docs directory for sidebar items to create a new collection. |
| 42 | */ |
| 43 | public function createSidebar(): self |
| 44 | { |
| 45 | $this->sidebar = new DocumentationSidebar(); |
| 46 | |
| 47 | foreach ($this->getSidebarItems() as $slug) { |
| 48 | $this->sidebar->addItem( |
| 49 | $this->createSidebarItemFromSlug($slug) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | return $this; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the sidebar object created and managed by the service instance. |
| 58 | */ |
| 59 | public function getSidebar(): DocumentationSidebar |
| 60 | { |
| 61 | return $this->sidebar; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the sorted sidebar created and managed by the service instance. |
| 66 | */ |
| 67 | public function getSortedSidebar(): DocumentationSidebar |
| 68 | { |
| 69 | return $this->getSidebar()->sortItems(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Add an item to the sidebar collection. |
| 74 | */ |
| 75 | public function addItem(DocumentationSidebarItem $item): self |
| 76 | { |
| 77 | $this->sidebar->addItem($item); |
| 78 | |
| 79 | return $this; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Remove the index page from the sidebar collection. |
| 84 | */ |
| 85 | protected function withoutIndex(): self |
| 86 | { |
| 87 | $this->sidebar = $this->sidebar->reject(function (DocumentationSidebarItem $item) { |
| 88 | return $item->destination === 'index'; |
| 89 | }); |
| 90 | |
| 91 | return $this; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Remove hidden files from the sidebar collection. |
| 96 | */ |
| 97 | protected function withoutHidden(): self |
| 98 | { |
| 99 | $this->sidebar = $this->sidebar->reject(function (DocumentationSidebarItem $item) { |
| 100 | return $item->isHidden(); |
| 101 | }); |
| 102 | |
| 103 | return $this; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get an array of source files to add to the sidebar. |
| 108 | */ |
| 109 | protected function getSidebarItems(): array |
| 110 | { |
| 111 | return CollectionService::getDocumentationPageList(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Generate a SidebarItem object from a source file referenced by its slug. |
| 116 | */ |
| 117 | protected function createSidebarItemFromSlug(string $slug): DocumentationSidebarItem |
| 118 | { |
| 119 | return DocumentationSidebarItem::parseFromFile($slug); |
| 120 | } |
| 121 | } |