ShopifyApp.php 5 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
use Meibuyu\Micro\Shopify\lib\Event;
17
use Meibuyu\Micro\Shopify\lib\Fulfillment;
18
use Meibuyu\Micro\Shopify\lib\FulfillmentOrder;
19
use Meibuyu\Micro\Shopify\lib\FulfillmentService;
20
use Meibuyu\Micro\Shopify\lib\Graphql;
21 22 23
use Meibuyu\Micro\Shopify\lib\InventoryItem;
use Meibuyu\Micro\Shopify\lib\InventoryLevel;
use Meibuyu\Micro\Shopify\lib\Location;
24
use Meibuyu\Micro\Shopify\lib\Metafield;
25
use Meibuyu\Micro\Shopify\lib\Order;
26 27
use Meibuyu\Micro\Shopify\lib\Product;
use Meibuyu\Micro\Shopify\lib\ProductVariant;
28
use Meibuyu\Micro\Shopify\lib\SmartCollection;
29 30 31 32 33 34 35
use Meibuyu\Micro\Shopify\lib\Webhook;

/**
 * Class ShopifyApp
 * @package Meibuyu\Shopify
 *
 * @property-read Webhook $Webhook
36 37 38 39 40
 * @property-read Collect $Collect
 * @property-read Collection $Collection
 * @property-read CustomCollection $CustomCollection
 * @property-read SmartCollection $SmartCollection
 * @property-read Metafield $Metafield
41 42
 * @property-read Product $Product
 * @property-read ProductVariant $ProductVariant
43 44 45
 * @property-read InventoryItem $InventoryItem
 * @property-read InventoryLevel $InventoryLevel
 * @property-read Location $Location
46
 * @property-read Order $Order
47
 * @property-read Event $Event
48
 * @property-read Fulfillment $Fulfillment
49
 * @property-read FulfillmentService $FulfillmentService
50
 * @property-read GraphQL $GraphQL
51 52 53 54 55 56
 *
 * @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)
57 58
 * @method Product Product(integer $id = null)
 * @method ProductVariant ProductVariant(integer $id = null)
59 60 61
 * @method InventoryItem InventoryItem(integer $id = null)
 * @method InventoryLevel InventoryLevel(integer $id = null)
 * @method Location Location(integer $id = null)
62
 * @method Order Order(integer $id = null)
63
 * @method Event Event(integer $id = null)
64
 * @method Fulfillment Fulfillment(integer $id = null)
65
 * @method FulfillmentOrder FulfillmentOrder()
66
 * @method FulfillmentService FulfillmentService(integer $id = null)
67
 * @method GraphQL GraphQL()
68
 *
69 70 71 72 73 74
 */
class ShopifyApp
{

    protected $resources = [
        'Webhook',
75 76 77 78 79
        'Collect',
        'Collection',
        'CustomCollection',
        'SmartCollection',
        'Metafield',
80 81
        'Product',
        'ProductVariant',
82 83 84
        'InventoryItem',
        'InventoryLevel',
        'Location',
85
        'Order',
86
        'Event',
87
        'Fulfillment',
88
        'FulfillmentOrder',
89
        'FulfillmentService',
90
        'GraphQL',
91 92
    ];

93 94 95
    protected $childResources = array(
        'Fulfillment' => 'Order',
        'FulfillmentEvent' => 'Fulfillment',
96
        'FulfillmentOrder' => 'Order',
97 98 99 100 101 102 103 104
        'OrderRisk' => 'Order',
        'ProductImage' => 'Product',
        'ProductVariant' => 'Product',
        'DiscountCode' => 'PriceRule',
        'Refund' => 'Order',
        'Transaction' => 'Order',
    );

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    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
130
     * @throws Exception
131 132
     */
    public function __get($className)
133 134 135 136 137 138 139 140 141 142 143 144
    {
        return $this->$className();
    }

    /**
     * 返回AbstractShopify实例
     * @param string $className 实现的类名
     * @param $arguments
     * @return AbstractShopify
     * @throws Exception
     */
    public function __call($className, $arguments)
145 146
    {
        if (!in_array($className, $this->resources)) {
147 148 149 150 151 152
            if (isset($this->childResources[$className])) {
                $message = "$className 是属于 {$this->childResources[$className]} 的子集, 无法直接访问";
            } else {
                $message = "未知类 $className";
            }
            throw new Exception($message);
153
        }
154 155

        $resourceID = !empty($arguments) ? $arguments[0] : null;
156
        $resourceClassName = __NAMESPACE__ . "\\lib\\$className";
157
        return new $resourceClassName($this->config, $resourceID);
158 159 160 161 162 163 164 165 166 167 168
    }

    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/";
    }

}