-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathIndexableElementModel.php
61 lines (55 loc) · 2.08 KB
/
IndexableElementModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace lhs\elasticsearch\models;
use Craft;
use craft\base\Element;
use craft\commerce\elements\Product;
use craft\digitalproducts\elements\Product as DigitalProduct;
use craft\elements\Asset;
use craft\elements\Entry;
use lhs\elasticsearch\exceptions\IndexableElementModelException;
/**
*
* @property-read Element $element
*/
class IndexableElementModel extends \craft\base\Model implements \JsonSerializable
{
public $elementId;
public $siteId;
public $type;
/**
* @return Element|null
* @throws IndexableElementModelException
*/
public function getElement()
{
switch ($this->type) {
case Product::class:
$commercePlugin = craft\commerce\Plugin::getInstance();
if (!$commercePlugin) {
throw new IndexableElementModelException($this, IndexableElementModelException::CRAFT_COMMERCE_NOT_INSTALLED);
}
$element = $commercePlugin->getProducts()->getProductById($this->elementId, $this->siteId);
break;
case DigitalProduct::class:
$digitalProductsPlugin = craft\digitalproducts\Plugin::getInstance();
if (!$digitalProductsPlugin) {
throw new IndexableElementModelException($this, IndexableElementModelException::DIGITAL_PRODUCTS_NOT_INSTALLED);
}
$element = Craft::$app->getElements()->getElementById($this->elementId, DigitalProduct::class, $this->siteId);
break;
case Entry::class:
$element = Craft::$app->getEntries()->getEntryById($this->elementId, $this->siteId);
break;
case Asset::class:
$element = Craft::$app->getAssets()->getAssetById($this->elementId, $this->siteId);
break;
default:
throw new IndexableElementModelException($this, IndexableElementModelException::UNEXPECTED_TYPE);
}
return $element;
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}