diff --git a/.travis.yml b/.travis.yml index b8d72f3..97aaa8b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,9 +12,9 @@ install: - echo "{\"http-basic\":{\"repo.magento.com\":{\"username\":\"${MAGENTO_USERNAME}\",\"password\":\"${MAGENTO_PASSWORD}\"}}}" > auth.json - composer install --prefer-dist script: - - sh -c "if [ '$TEST_SUITE' = 'phpcs' ]; then php vendor/bin/phpcs --standard=PSR2 Model/ Console/ Test/; fi" - - sh -c "if [ '$TEST_SUITE' = 'phpcs' ]; then php vendor/bin/phpmd Model/,Console/,Test/ text cleancode,codesize,controversial,design,naming,unusedcode; fi" - - sh -c "if [ '$TEST_SUITE' = 'phpcs' ]; then php vendor/bin/phpcpd Model/ Console/ Test/; fi" + - sh -c "if [ '$TEST_SUITE' = 'phpcs' ]; then php vendor/bin/phpcs --standard=PSR2 Model/ Console/ Test/ Helper/; fi" + - sh -c "if [ '$TEST_SUITE' = 'phpcs' ]; then php vendor/bin/phpmd Model/,Console/,Test/,Helper/ text cleancode,codesize,controversial,design,naming,unusedcode; fi" + - sh -c "if [ '$TEST_SUITE' = 'phpcs' ]; then php vendor/bin/phpcpd Model/ Console/ Test/ Helper/; fi" - sh -c "if [ '$TEST_SUITE' = 'unit' ]; then php vendor/bin/phpunit --coverage-clover build/logs/clover.xml Test/Unit/; fi" notifications: hipchat: diff --git a/Helper/Component.php b/Helper/Component.php index a351ea3..b8928f5 100644 --- a/Helper/Component.php +++ b/Helper/Component.php @@ -51,5 +51,4 @@ public function getStoreByCode($code) return $store; } - -} \ No newline at end of file +} diff --git a/Model/Component/Widgets.php b/Model/Component/Widgets.php new file mode 100644 index 0000000..f200652 --- /dev/null +++ b/Model/Component/Widgets.php @@ -0,0 +1,214 @@ +widgetCollection = $collection; + $this->themeCollection = $themeCollection; + $this->storeFactory = $storeFactory; + } + + protected function processData($data = null) + { + try { + foreach ($data as $widgetData) { + $this->processWidget($widgetData); + } + } catch (ComponentException $e) { + $this->log->logError($e->getMessage()); + } + } + + public function processWidget($widgetData) + { + try { + $this->validateInstanceType($widgetData['instance_type']); + + $widget = $this->findWidgetByInstanceTypeAndTitle($widgetData['instance_type'], $widgetData['title']); + + $canSave = false; + if (is_null($widget)) { + $canSave = true; + $widget = $this->objectManager->create(\Magento\Widget\Model\Widget\Instance::class); + } + + foreach ($widgetData as $key => $value) { + + // @todo handle stores + // Comma separated + if ($key == "stores") { + $key = "store_ids"; + $value = $this->getCommaSeparatedStoreIds($value); + } + + if ($key == "parameters") { + $key = "widget_parameters"; + $value = $this->populateWidgetParameters($value); + } + + if ($key == "theme") { + $key = "theme_id"; + $value = $this->getThemeId($value); + } + + if ($widget->getData($key) == $value) { + $this->log->logComment(sprintf("Widget %s = %s", $key, $value), 1); + continue; + } + + $canSave = true; + $widget->setData($key, $value); + $this->log->logInfo(sprintf("Widget %s = %s", $key, $value), 1); + } + + if ($canSave) { + $widget->save(); + $this->log->logInfo(sprintf("Saved Widget %s", $widget->getTitle()), 1); + } + + } catch (ComponentException $e) { + $this->log->logError($e->getMessage()); + } + } + + public function validateInstanceType($instanceType) + { + $this->log->logComment(sprintf("Checking if %s is a valid instance", $instanceType)); + $instanceType = '\\' . $instanceType; + $instance = $this->objectManager->create($instanceType); + if (!$instance instanceof $instanceType) { + throw new ComponentException("Instance %s is invalid", $instanceType); + } + $this->log->logComment(sprintf("Found instance %s.", $instanceType)); + // @todo validate parameters somehow using the $fields + } + + /** + * @param $widgetInstanceType + * @param $widgetTitle + * @return \Magento\Framework\DataObject|null + * @throws ComponentException + * @todo get this one to work instead of findWidgetByInstanceTypeAndTitle() + */ + public function getWidgetByInstanceTypeAndTitle($widgetInstanceType, $widgetTitle) + { + + // Clear any existing filters applied to the widget collection + $this->widgetCollection->getSelect()->reset(\Zend_Db_Select::WHERE); + $this->widgetCollection->removeAllItems(); + + // Filter widget collection + $widgets = $this->widgetCollection + ->addFieldToFilter('instance_type', $widgetInstanceType) + ->addFieldToFilter('title', $widgetTitle) + ->load(); + // @todo add store filter + + + // If we have more than 1, throw an exception for now. Needs store filter to drill down the widgets further + // into a single widget. + if ($widgets->count() > 1) { + throw new ComponentException('Application Error: Need to figure out how to handle same titled widgets'); + } + + // If there are no widgets, then it is like it doesn't even exist. + // Return null + if ($widgets->count() < 1) { + return null; + } + + // Return the widget itself since it is a perfect match + return $widgets->getFirstItem(); + } + + /** + * @param $widgetInstanceType + * @param $widgetTitle + * @return mixed|null + */ + public function findWidgetByInstanceTypeAndTitle($widgetInstanceType, $widgetTitle) + { + + // Loop through the widget collection to find any matches. + foreach ($this->widgetCollection as $widget) { + if ($widget->getTitle() == $widgetTitle && $widget->getInstanceType() == $widgetInstanceType) { + + // Return the widget if there is a match + return $widget; + } + } + + // If there are no widgets, then it is like it doesn't even exist. + // Return null + return null; + } + + public function getThemeId($themeCode) + { + + // Filter Theme Collection + $themes = $this->themeCollection->addFilter('code', $themeCode); + + if ($themes->count() == 0) { + throw new ComponentException(sprintf('Could not find any themes with the theme code %s', $themeCode)); + } + + $theme = $themes->getFirstItem(); + + return $theme->getId(); + } + + /** + * @param array $parameters + * @return string + * @todo better support with parameters that reference IDs of objects + */ + public function populateWidgetParameters(array $parameters) + { + + // Default property return + return serialize($parameters); + } + + /** + * @param $stores + * @return string + */ + public function getCommaSeparatedStoreIds($stores) + { + $storeIds = array(); + foreach ($stores as $code) { + $storeView = $this->storeFactory->create(); + $storeView->load($code, 'code'); + if (!$storeView->getId()) { + throw new ComponentException(sprintf('Cannot find store with code %s', $code)); + } + $storeIds[] = $storeView->getId(); + } + return implode(',', $storeIds); + } +} diff --git a/README.md b/README.md index 08e5a5a..7745d97 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,9 @@ This is a work in progress and by no means for use with production environments If you are contributing the module, please run the following commands to stand the best chance with Travis CI liking your code. These test include PHP Code Sniffer, PHP Mess Detector, PHP Copy and Paste Detector, PHP Unit ``` -php vendor/bin/phpcs --standard=PSR2 vendor/ctidigital/magento2-configurator/Model/ vendor/ctidigital/magento2-configurator/Console/ vendor/ctidigital/magento2-configurator/Test/ -php vendor/bin/phpmd vendor/ctidigital/magento2-configurator/Model/,vendor/ctidigital/magento2-configurator/Console/,vendor/ctidigital/magento2-configurator/Test/ text cleancode,codesize,controversial,design,naming,unusedcode -php vendor/bin/phpcpd vendor/ctidigital/magento2-configurator/Model/ vendor/ctidigital/magento2-configurator/Console vendor/ctidigital/magento2-configurator/Test/ +php vendor/bin/phpcs --standard=PSR2 vendor/ctidigital/magento2-configurator/Model/ vendor/ctidigital/magento2-configurator/Console/ vendor/ctidigital/magento2-configurator/Test/ vendor/ctidigital/magento2-configurator/Helper/ +php vendor/bin/phpmd vendor/ctidigital/magento2-configurator/Model/,vendor/ctidigital/magento2-configurator/Console/,vendor/ctidigital/magento2-configurator/Test/,vendor/ctidigital/magento2-configurator/Helper/ text cleancode,codesize,controversial,design,naming,unusedcode +php vendor/bin/phpcpd vendor/ctidigital/magento2-configurator/Model/ vendor/ctidigital/magento2-configurator/Console vendor/ctidigital/magento2-configurator/Test/ vendor/ctidigital/magento2-configurator/Helper/ php vendor/bin/phpunit --coverage-clover build/logs/clover.xml vendor/ctidigital/magento2-configurator/Test/Unit/ ``` @@ -40,32 +40,33 @@ Do also include sample files with your component that works ## Roadmap for components to do -| Component | Code Written | Tests Written | Sample Files | -|---------------------------|--------------------|---------------|--------------------| -| Websites | :white_check_mark: | :x: | :white_check_mark: | -| System Configuration | :white_check_mark: | :x: | :white_check_mark: | -| Blocks | :white_check_mark: | :x: | :white_check_mark: | -| Attribute Sets | :x: | :x: | :x: | -| Attributes | :x: | :x: | :x: | -| Categories | :x: | :x: | :x: | -| Products | :x: | :x: | :x: | -| Admin Roles | :white_check_mark: | :x: | :white_check_mark: | -| Admin Users | :white_check_mark: | :x: | :white_check_mark: | -| Pages | :white_check_mark: | :x: | :white_check_mark: | -| Customers | :x: | :x: | :x: | -| Media | :x: | :x: | :x: | -| Widgets | :x: | :x: | :x: | -| Related Products | :x: | :x: | :x: | -| SQL | :x: | :x: | :x: | -| Customer Groups | :white_check_mark: | :x: | :white_check_mark: | -| Tax Rules | :x: | :x: | :x: | -| API Roles | :x: | :x: | :x: | -| API Users | :x: | :x: | :x: | -| Shipping Table Rates | :x: | :x: | :x: | -| Catalog Price Rules | :x: | :x: | :x: | -| Shopping Cart Price Rules | :x: | :x: | :x: | -| Rewrites | :x: | :x: | :x: | -| Orders | :x: | :x: | :x: | +| Component | Code Written | Tests Written | Sample Files | +|---------------------------|--------------------|--------------------|--------------------| +| Websites | :white_check_mark: | :grey_exclamation: | :white_check_mark: | +| System Configuration | :white_check_mark: | :grey_exclamation: | :white_check_mark: | +| Blocks | :white_check_mark: | :grey_exclamation: | :white_check_mark: | +| Admin Roles | :white_check_mark: | :grey_exclamation: | :white_check_mark: | +| Admin Users | :white_check_mark: | :grey_exclamation: | :white_check_mark: | +| Pages | :white_check_mark: | :grey_exclamation: | :white_check_mark: | +| Widgets | :white_check_mark: | :grey_exclamation: | :white_check_mark: | +| Customer Groups | :white_check_mark: | :grey_exclamation: | :white_check_mark: | +| Attribute Sets | :x: | :x: | :x: | +| Attributes | :x: | :x: | :x: | +| Categories | :x: | :x: | :x: | +| Products | :x: | :x: | :x: | +| Customers | :x: | :x: | :x: | +| Media | :x: | :x: | :x: | +| Related Products | :x: | :x: | :x: | +| SQL | :x: | :x: | :x: | +| Tax Rules | :x: | :x: | :x: | +| Customers | :x: | :x: | :x: | +| API Roles | :x: | :x: | :x: | +| API Users | :x: | :x: | :x: | +| Shipping Table Rates | :x: | :x: | :x: | +| Catalog Price Rules | :x: | :x: | :x: | +| Shopping Cart Price Rules | :x: | :x: | :x: | +| Rewrites | :x: | :x: | :x: | +| Orders | :x: | :x: | :x: | License ---- diff --git a/Samples/Components/Widgets/widgets.yaml b/Samples/Components/Widgets/widgets.yaml new file mode 100644 index 0000000..86f1ed2 --- /dev/null +++ b/Samples/Components/Widgets/widgets.yaml @@ -0,0 +1,19 @@ +- + instance_type: Magento\Cms\Block\Widget\Page\Link + title: Test Link + theme: Magento/blank + stores: + - default + - usa_en_us + parameters: + anchor_text: Anchor Test + title: Anchor Title + page_id: 4 +- + instance_type: Magento\Cms\Block\Widget\Block + title: Test Block + theme: Magento/blank + stores: + - default + parameters: + block_id: 13 \ No newline at end of file diff --git a/Samples/master.yaml b/Samples/master.yaml index d4e0c43..23553fd 100644 --- a/Samples/master.yaml +++ b/Samples/master.yaml @@ -46,6 +46,24 @@ blocks: local: mode: maintain log: debug +pages: + enabled: 1 + method: code + sources: + - ../configurator/Blocks/pages.yaml + env: + local: + mode: maintain + log: debug +widgets: + enabled: 1 + method: code + sources: + - ../configurator/Widgets/widgets.yaml + env: + local: + mode: maintain + log: debug customergroups: enabled: 1 method: code diff --git a/Test/Unit/Component/WidgetsTest.php b/Test/Unit/Component/WidgetsTest.php new file mode 100644 index 0000000..0e80a9e --- /dev/null +++ b/Test/Unit/Component/WidgetsTest.php @@ -0,0 +1,28 @@ +getMock(StoreFactory::class); + $widgetCollection = $this->getMock(WidgetCollection::class, [], [], '', false); + $themeCollection = $this->getMock(ThemeCollection::class, [], [], '', false); + + $this->component = new Widgets( + $this->logInterface, + $this->objectManager, + $widgetCollection, + $storeFactory, + $themeCollection + ); + $this->className = Widgets::class; + } +} diff --git a/composer.json b/composer.json index e39abe2..b94dffe 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "satooshi/php-coveralls": "^1.0", "phpunit/phpunit": "4.1.0" }, - "version": "0.7.1-dev", + "version": "0.8.0-dev", "autoload": { "files": [ "registration.php" ], "psr-4": { diff --git a/etc/configurator.xml b/etc/configurator.xml index 6af33f9..52f9d2d 100644 --- a/etc/configurator.xml +++ b/etc/configurator.xml @@ -11,4 +11,5 @@ + \ No newline at end of file