ShopifyApp.php 4.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2020/8/18
 * Time: 8:13
 */

namespace Meibuyu\Micro\Shopify;

11
use Exception;
12
use Meibuyu\Micro\Shopify\lib\AbstractShopify;
13 14 15
use Meibuyu\Micro\Shopify\lib\Collect;
use Meibuyu\Micro\Shopify\lib\Collection;
use Meibuyu\Micro\Shopify\lib\CustomCollection;
16 17 18
use Meibuyu\Micro\Shopify\lib\InventoryItem;
use Meibuyu\Micro\Shopify\lib\InventoryLevel;
use Meibuyu\Micro\Shopify\lib\Location;
19
use Meibuyu\Micro\Shopify\lib\Metafield;
20
use Meibuyu\Micro\Shopify\lib\Order;
21 22
use Meibuyu\Micro\Shopify\lib\Product;
use Meibuyu\Micro\Shopify\lib\ProductVariant;
23
use Meibuyu\Micro\Shopify\lib\SmartCollection;
24 25 26 27 28 29 30
use Meibuyu\Micro\Shopify\lib\Webhook;

/**
 * Class ShopifyApp
 * @package Meibuyu\Shopify
 *
 * @property-read Webhook $Webhook
31 32 33 34 35
 * @property-read Collect $Collect
 * @property-read Collection $Collection
 * @property-read CustomCollection $CustomCollection
 * @property-read SmartCollection $SmartCollection
 * @property-read Metafield $Metafield
36 37
 * @property-read Product $Product
 * @property-read ProductVariant $ProductVariant
38 39 40
 * @property-read InventoryItem $InventoryItem
 * @property-read InventoryLevel $InventoryLevel
 * @property-read Location $Location
41
 * @property-read Order $Order
42 43 44 45 46 47
 *
 * @method Webhook Webhook(integer $id = null)
 * @method Collection Collection(integer $id = null)
 * @method CustomCollection CustomCollection(integer $id = null)
 * @method SmartCollection SmartCollection(integer $id = null)
 * @method Metafield Metafield(integer $id = null)
48 49
 * @method Product Product(integer $id = null)
 * @method ProductVariant ProductVariant(integer $id = null)
50 51 52
 * @method InventoryItem InventoryItem(integer $id = null)
 * @method InventoryLevel InventoryLevel(integer $id = null)
 * @method Location Location(integer $id = null)
53
 * @method Order Order(integer $id = null)
54
 *
55 56 57 58 59 60
 */
class ShopifyApp
{

    protected $resources = [
        'Webhook',
61 62 63 64 65
        'Collect',
        'Collection',
        'CustomCollection',
        'SmartCollection',
        'Metafield',
66 67
        'Product',
        'ProductVariant',
68 69 70
        'InventoryItem',
        'InventoryLevel',
        'Location',
71
        'Order',
72 73
    ];

74 75 76 77 78 79 80 81 82 83 84
    protected $childResources = array(
        'Fulfillment' => 'Order',
        'FulfillmentEvent' => 'Fulfillment',
        'OrderRisk' => 'Order',
        'ProductImage' => 'Product',
        'ProductVariant' => 'Product',
        'DiscountCode' => 'PriceRule',
        'Refund' => 'Order',
        'Transaction' => 'Order',
    );

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    public $config = [];

    public $defaultApiVersion = '2020-07';

    /**
     * ShopifyApp constructor.
     * @param array $config
     */
    public function __construct($config)
    {
        $this->config = [
            'api_version' => $this->defaultApiVersion
        ];
        foreach ($config as $key => $value) {
            $this->config[$key] = $value;
        }
        if (isset($config['shop_url'])) {
            $this->setApiUrl();
        }
    }

    /**
     * 返回AbstractShopify实例
     * @param string $className 实现的类名
     * @return AbstractShopify
110
     * @throws Exception
111 112
     */
    public function __get($className)
113 114 115 116 117 118 119 120 121 122 123 124
    {
        return $this->$className();
    }

    /**
     * 返回AbstractShopify实例
     * @param string $className 实现的类名
     * @param $arguments
     * @return AbstractShopify
     * @throws Exception
     */
    public function __call($className, $arguments)
125 126
    {
        if (!in_array($className, $this->resources)) {
127 128 129 130 131 132
            if (isset($this->childResources[$className])) {
                $message = "$className 是属于 {$this->childResources[$className]} 的子集, 无法直接访问";
            } else {
                $message = "未知类 $className";
            }
            throw new Exception($message);
133
        }
134 135

        $resourceID = !empty($arguments) ? $arguments[0] : null;
136
        $resourceClassName = __NAMESPACE__ . "\\lib\\$className";
137
        return new $resourceClassName($this->config, $resourceID);
138 139 140 141 142 143 144 145 146 147 148
    }

    public function setApiUrl()
    {
        $shopUrl = $this->config['shop_url'];
        $shopUrl = preg_replace('#^https?://|/$#', '', $shopUrl);
        $apiVersion = $this->config['api_version'];
        $this->config['api_url'] = "https://$shopUrl/admin/api/$apiVersion/";
    }

}