diff --git a/.github/workflows/analyse.yml b/.github/workflows/analyse.yml new file mode 100644 index 0000000..c687e18 --- /dev/null +++ b/.github/workflows/analyse.yml @@ -0,0 +1,25 @@ +name: PHPStan + +on: ['push', 'pull_request'] + +jobs: + test: + runs-on: ubuntu-latest + name: analyse + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + coverage: none + + - name: Install dependencies + run: composer install --no-interaction + + - name: Analyse + run: vendor/bin/phpstan analyse \ No newline at end of file diff --git a/composer.json b/composer.json index 6428e04..be40333 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,22 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "php": "^8.0.2|^8.1" + "php": "^8.0.2|^8.1", + "magento/framework": "*", + "magento/module-sales-rule": "*", + "magento/module-catalog": "*", + "magento/module-graph-ql": "*", + "magento/module-quote-graph-ql": "*" + }, + "require-dev": { + "bitexpert/phpstan-magento": "^0.11.0", + "phpstan/phpstan": "^1.10" + }, + "repositories": { + "magento": { + "type": "composer", + "url": "https://repo-magento-mirror.fooman.co.nz/" + } }, "autoload": { "psr-4": { @@ -19,6 +34,9 @@ ] }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "magento/composer-dependency-version-audit-plugin": true + } } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..3d73f2b --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,9 @@ +includes: + - vendor/bitexpert/phpstan-magento/extension.neon +parameters: + paths: + - . + excludePaths: + - vendor + - Test/* + level: 5 \ No newline at end of file diff --git a/src/Model/Resolver/Inventory/StockItem.php b/src/Model/Resolver/Inventory/StockItem.php index 497c9c5..6b899d8 100644 --- a/src/Model/Resolver/Inventory/StockItem.php +++ b/src/Model/Resolver/Inventory/StockItem.php @@ -32,7 +32,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value /** @var ProductInterface $product */ $product = $value['model']; /** @var StockStatusInterface $stockStatus */ - $stockStatus = $this->stockStatusRepository->get($product->getId()); + $stockStatus = $this->stockStatusRepository->get((string) $product->getId()); /** @var StockItemInterface $stockItem */ $stockItem = $stockStatus->getStockItem(); diff --git a/src/Model/Resolver/Quote/Backorder.php b/src/Model/Resolver/Quote/Backorder.php index 3ede856..cdb67f1 100644 --- a/src/Model/Resolver/Quote/Backorder.php +++ b/src/Model/Resolver/Quote/Backorder.php @@ -39,7 +39,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value if ($configuredItems) { /** @var Option $item */ $item = reset($configuredItems); - $backorderStatus = $item?->getProduct()?->getExtensionAttributes()?->getStockItem()?->getBackorders() ?? 0; + // @phpstan-ignore-next-line + $backorderStatus = $item?->getProduct()?->getExtensionAttributes()->getStockItem()->getBackorders() ?? 0; if ($backorderStatus != 2) { return 0; } @@ -48,6 +49,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value /** @var ProductInterface $product */ $product = $this->productRepositoryInterface->get($cartItem->getSku()); + // @phpstan-ignore-next-line $stockItem = $product->getExtensionAttributes()->getStockItem(); if (!$stockItem || $stockItem->getBackorders() != 2) { return 0; diff --git a/src/Model/Resolver/Quote/Data/SalesRuleLabel.php b/src/Model/Resolver/Quote/Data/SalesRuleLabel.php new file mode 100644 index 0000000..f6408e3 --- /dev/null +++ b/src/Model/Resolver/Quote/Data/SalesRuleLabel.php @@ -0,0 +1,54 @@ +getAppliedRuleIds()) as $key=>$ruleId) { + /** @var RuleInterface $rule */ + $rule = $this->ruleRepository->getById((int) $ruleId); + + // @phpstan-ignore-next-line + $store = $context->getExtensionAttributes()->getStore(); + $storeId = $store->getId(); + + foreach($rule->getStoreLabels() as $storeLabel) { + if ((int) $storeLabel->getStoreId() === (int) $storeId) { + $storeLabel = $storeLabel->getStoreLabel(); + break; + } + } + + $labels[] = [ + 'name' => $rule->getName(), + 'description' => $rule->getDescription(), + 'discount_amount' => $rule->getDiscountAmount(), + 'from_date' => $rule->getFromDate(), + 'to_date' => $rule->getToDate(), + 'store_label' => $storeLabel ?? null, + ]; + } + + return $labels; + } +} \ No newline at end of file diff --git a/src/etc/module.xml b/src/etc/module.xml index 4c3c07c..2c3f238 100644 --- a/src/etc/module.xml +++ b/src/etc/module.xml @@ -3,8 +3,10 @@ xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> + + \ No newline at end of file diff --git a/src/etc/schema.graphqls b/src/etc/schema.graphqls index 27e534a..c91125a 100644 --- a/src/etc/schema.graphqls +++ b/src/etc/schema.graphqls @@ -10,6 +10,16 @@ type ProductStockItem { qty_increments: Int @doc(description: "Quantity increments in which you can buy the item") } +type SalesRuleLabel { + name: String @doc(description: "Name of the label") + description: String @doc(description: "Description of the label") + store_label: String @doc(description: "Label of the current store") + discount_amount: Float @doc(description: "Amount of discount") + from_date: String @doc(description: "Date the sales rule becomes active") + to_date: String @doc(description: "Date the sales rule stops being active") +} + interface CartItemInterface { qty_backordered: Int @resolver(class: "Rapidez\\Compadre\\Model\\Resolver\\Quote\\Backorder") @doc(description: "Amount of items that are backordered") + sales_rule_labels: [SalesRuleLabel] @resolver(class: "Rapidez\\Compadre\\Model\\Resolver\\Quote\\Data\\SalesRuleLabel") @doc(descipriont: "Labels of the sales rules") }