Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| Sitekit | |
0.00% |
0 / 43 |
|
0.00% |
0 / 12 |
420 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| validate_parameters | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
| set_properties | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| get_slug | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_title | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_header | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_footer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_pages | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_color_palette | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_page_content | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onboarding_preview_data | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| to_array | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Onboarding\Types; |
| 4 | |
| 5 | /** |
| 6 | * Sitekit data type class. |
| 7 | * |
| 8 | * @package NewfoldLabs\WP\Module\Onboarding\Types |
| 9 | */ |
| 10 | class Sitekit { |
| 11 | |
| 12 | /** |
| 13 | * Sitekit slug. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | private $slug; |
| 18 | |
| 19 | /** |
| 20 | * Sitekit title. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | private $title; |
| 25 | |
| 26 | /** |
| 27 | * Sitekit header. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | private $header; |
| 32 | |
| 33 | /** |
| 34 | * Sitekit footer. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | private $footer; |
| 39 | |
| 40 | /** |
| 41 | * Sitekit pages. |
| 42 | * |
| 43 | * @var Pages |
| 44 | */ |
| 45 | private $pages; |
| 46 | |
| 47 | /** |
| 48 | * Sitekit color palette. |
| 49 | * |
| 50 | * @var ColorPalette|null |
| 51 | */ |
| 52 | private $color_palette; |
| 53 | |
| 54 | /** |
| 55 | * Sitekit constructor. |
| 56 | * |
| 57 | * @param string $slug Sitekit slug. |
| 58 | * @param string $title Sitekit title. |
| 59 | * @param string $header Sitekit header. |
| 60 | * @param string $footer Sitekit footer. |
| 61 | * @param Pages $pages Pages collection. |
| 62 | * @param ColorPalette|null $color_palette Color palette (optional). |
| 63 | * @throws \InvalidArgumentException When parameters are invalid. |
| 64 | */ |
| 65 | public function __construct( string $slug, string $title, string $header, string $footer, Pages $pages, ColorPalette $color_palette = null ) { |
| 66 | $this->validate_parameters( $slug, $title, $header, $footer, $pages ); |
| 67 | $this->set_properties( $slug, $title, $header, $footer, $pages, $color_palette ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Validate constructor parameters. |
| 72 | * |
| 73 | * @param string $slug Sitekit slug. |
| 74 | * @param string $title Sitekit title. |
| 75 | * @param string $header Sitekit header. |
| 76 | * @param string $footer Sitekit footer. |
| 77 | * @param Pages $pages Pages collection. |
| 78 | * @throws \InvalidArgumentException When parameters are invalid. |
| 79 | */ |
| 80 | private function validate_parameters( string $slug, string $title, string $header, string $footer, Pages $pages ): void { |
| 81 | if ( empty( trim( $slug ) ) ) { |
| 82 | throw new \InvalidArgumentException( 'Slug cannot be empty' ); |
| 83 | } |
| 84 | |
| 85 | if ( empty( trim( $title ) ) ) { |
| 86 | throw new \InvalidArgumentException( 'Title cannot be empty' ); |
| 87 | } |
| 88 | |
| 89 | if ( empty( trim( $header ) ) ) { |
| 90 | throw new \InvalidArgumentException( 'Header cannot be empty' ); |
| 91 | } |
| 92 | |
| 93 | if ( empty( trim( $footer ) ) ) { |
| 94 | throw new \InvalidArgumentException( 'Footer cannot be empty' ); |
| 95 | } |
| 96 | |
| 97 | // Validate that pages collection has at least one page with slug "home". |
| 98 | if ( $pages->is_empty() || ! $pages->has_page( 'home' ) ) { |
| 99 | throw new \InvalidArgumentException( 'Pages collection must contain at least one page with slug \'home\'' ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Set properties from parameters. |
| 105 | * |
| 106 | * @param string $slug Sitekit slug. |
| 107 | * @param string $title Sitekit title. |
| 108 | * @param string $header Sitekit header. |
| 109 | * @param string $footer Sitekit footer. |
| 110 | * @param Pages $pages Pages collection. |
| 111 | * @param ColorPalette|null $color_palette Color palette. |
| 112 | */ |
| 113 | private function set_properties( string $slug, string $title, string $header, string $footer, Pages $pages, ColorPalette $color_palette = null ): void { |
| 114 | $this->slug = trim( $slug ); |
| 115 | $this->title = trim( $title ); |
| 116 | $this->header = trim( $header ); |
| 117 | $this->footer = trim( $footer ); |
| 118 | $this->pages = $pages; |
| 119 | $this->color_palette = $color_palette; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get slug. |
| 124 | * |
| 125 | * @return string |
| 126 | */ |
| 127 | public function get_slug(): string { |
| 128 | return $this->slug; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get title. |
| 133 | * |
| 134 | * @return string |
| 135 | */ |
| 136 | public function get_title(): string { |
| 137 | return $this->title; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get header. |
| 142 | * |
| 143 | * @return string |
| 144 | */ |
| 145 | public function get_header(): string { |
| 146 | return $this->header; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get footer. |
| 151 | * |
| 152 | * @return string |
| 153 | */ |
| 154 | public function get_footer(): string { |
| 155 | return $this->footer; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get pages collection. |
| 160 | * |
| 161 | * @return Pages |
| 162 | */ |
| 163 | public function get_pages(): Pages { |
| 164 | return $this->pages; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get color palette. |
| 169 | * |
| 170 | * @return ColorPalette|null |
| 171 | */ |
| 172 | public function get_color_palette() { |
| 173 | return $this->color_palette; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Get page content by page key. |
| 178 | * |
| 179 | * @param string $page_key The page key to retrieve content for. |
| 180 | * @return string|null |
| 181 | */ |
| 182 | public function get_page_content( string $page_key ) { |
| 183 | return $this->pages->get_page_content_by_slug( $page_key ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Get data for onboarding preview. |
| 188 | * |
| 189 | * @return array |
| 190 | */ |
| 191 | public function onboarding_preview_data(): array { |
| 192 | return array( |
| 193 | 'slug' => $this->slug, |
| 194 | 'title' => $this->title, |
| 195 | 'header' => $this->header, |
| 196 | 'footer' => $this->footer, |
| 197 | 'content' => $this->pages->get_front_page()->get_content(), |
| 198 | 'color' => $this->color_palette ? $this->color_palette->to_array() : null, |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Convert to array. |
| 204 | * |
| 205 | * @return array |
| 206 | */ |
| 207 | public function to_array(): array { |
| 208 | $data = array( |
| 209 | 'slug' => $this->slug, |
| 210 | 'title' => $this->title, |
| 211 | 'header' => $this->header, |
| 212 | 'footer' => $this->footer, |
| 213 | 'pages' => $this->pages->to_associative_array(), |
| 214 | ); |
| 215 | |
| 216 | if ( null !== $this->color_palette ) { |
| 217 | $data['color_palette'] = $this->color_palette->to_array(); |
| 218 | } |
| 219 | |
| 220 | return $data; |
| 221 | } |
| 222 | } |