Skip to content

Commit

Permalink
Merge pull request #15 from ctidigital/widgets
Browse files Browse the repository at this point in the history
Widgets component
  • Loading branch information
chevli authored Mar 29, 2017
2 parents 51b3e2e + 51b8e7c commit ffc846a
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 35 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions Helper/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,4 @@ public function getStoreByCode($code)

return $store;
}

}
}
214 changes: 214 additions & 0 deletions Model/Component/Widgets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
<?php

namespace CtiDigital\Configurator\Model\Component;

use CtiDigital\Configurator\Model\LoggingInterface;
use Magento\Framework\ObjectManagerInterface;
use CtiDigital\Configurator\Model\Exception\ComponentException;
use Magento\Widget\Model\ResourceModel\Widget\Instance\Collection as WidgetCollection;
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
use Magento\Store\Model\StoreFactory;

class Widgets extends YamlComponentAbstract
{

protected $alias = 'widgets';
protected $name = 'Widgets';
protected $description = 'Component to manage CMS Widgets';
protected $widgetCollection;
protected $themeCollection;
protected $storeFactory;

public function __construct(
LoggingInterface $log,
ObjectManagerInterface $objectManager,
WidgetCollection $collection,
StoreFactory $storeFactory,
ThemeCollection $themeCollection
) {
parent::__construct($log, $objectManager);
$this->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);
}
}
59 changes: 30 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
```

Expand All @@ -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
----
Expand Down
19 changes: 19 additions & 0 deletions Samples/Components/Widgets/widgets.yaml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions Samples/master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions Test/Unit/Component/WidgetsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace CtiDigital\Configurator\Test\Unit\Component;

use CtiDigital\Configurator\Model\Component\Widgets;
use Magento\Store\Model\StoreFactory;
use Magento\Widget\Model\ResourceModel\Widget\Instance\Collection as WidgetCollection;
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
use Psr\Log\LoggerInterface;

class WidgetsTest extends ComponentAbstractTestCase
{
protected function componentSetUp()
{
$storeFactory = $this->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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions etc/configurator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
<component name="products" class="CtiDigital\Configurator\Model\Component\Products" />
<component name="blocks" class="CtiDigital\Configurator\Model\Component\Blocks" />
<component name="pages" class="CtiDigital\Configurator\Model\Component\Pages" />
<component name="widgets" class="CtiDigital\Configurator\Model\Component\Widgets" />
</config>

0 comments on commit ffc846a

Please sign in to comment.