-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from lizardmedia/develop
Develop
- Loading branch information
Showing
15 changed files
with
6,586 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* File:MobileDetectorInterface.php | ||
* | ||
* @author Maciej Sławik <maciej.slawik@lizardmedia.pl> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\CartRuleMobile\Api; | ||
|
||
/** | ||
* Interface MobileDetectorInterface | ||
* @package LizardMedia\CartRuleMobile\Api | ||
*/ | ||
interface MobileDetectorInterface | ||
{ | ||
/** | ||
* @return bool | ||
*/ | ||
public function isMobileDevice(): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* File:MobileDetector.php | ||
* | ||
* @author Maciej Sławik <maciej.slawik@lizardmedia.pl> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\CartRuleMobile\Model; | ||
|
||
use LizardMedia\CartRuleMobile\Api\MobileDetectorInterface; | ||
use Zend_Http_UserAgent; | ||
|
||
/** | ||
* Class MobileDetector | ||
* @package LizardMedia\CartRuleMobile\Model | ||
*/ | ||
class MobileDetector implements MobileDetectorInterface | ||
{ | ||
/** | ||
* @var Zend_Http_UserAgent | ||
*/ | ||
private $userAgent; | ||
|
||
/** | ||
* MobileDetector constructor. | ||
* @param Zend_Http_UserAgent $userAgent | ||
*/ | ||
public function __construct( | ||
Zend_Http_UserAgent $userAgent | ||
) { | ||
$this->userAgent = $userAgent; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isMobileDevice(): bool | ||
{ | ||
return (bool)preg_match( | ||
'/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i', | ||
$this->userAgent->getUserAgent() | ||
); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* File:Mobile.php | ||
* | ||
* @author Maciej Sławik <maciej.slawik@lizardmedia.pl> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\CartRuleMobile\Model\Rule\Condition; | ||
|
||
use LizardMedia\CartRuleMobile\Api\MobileDetectorInterface; | ||
use Magento\Config\Model\Config\Source\Yesno; | ||
use Magento\Framework\Model\AbstractModel; | ||
use Magento\Rule\Model\Condition\AbstractCondition; | ||
use Magento\Rule\Model\Condition\Context; | ||
|
||
/** | ||
* Class Mobile | ||
* @package LizardMedia\CartRuleMobile\Model\Rule\Condition | ||
*/ | ||
class Mobile extends AbstractCondition | ||
{ | ||
/** | ||
* @var Yesno | ||
*/ | ||
protected $sourceYesNo; | ||
|
||
/** | ||
* @var MobileDetectorInterface | ||
*/ | ||
private $mobileDetector; | ||
|
||
/** | ||
* Constructor | ||
* @param Context $context | ||
* @param Yesno $sourceYesNo | ||
* @param MobileDetectorInterface $mobileDetector | ||
* @param array $data | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
Yesno $sourceYesNo, | ||
MobileDetectorInterface $mobileDetector, | ||
array $data = [] | ||
) { | ||
$this->callParentConstructor($context, $data); | ||
$this->sourceYesNo = $sourceYesNo; | ||
$this->mobileDetector = $mobileDetector; | ||
} | ||
|
||
/** | ||
* @param AbstractModel $model | ||
* @return bool | ||
*/ | ||
public function validate(AbstractModel $model) | ||
{ | ||
$enabledOnMobile = (bool)$this->getValue(); | ||
$isMobile = $this->mobileDetector->isMobileDevice(); | ||
|
||
return $enabledOnMobile === $isMobile; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getInputType() | ||
{ | ||
return 'select'; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getValueElementType() | ||
{ | ||
return 'select'; | ||
} | ||
|
||
/** | ||
* @return array|mixed | ||
*/ | ||
public function getValueSelectOptions() | ||
{ | ||
if (!$this->hasData('value_select_options')) { | ||
$this->setData( | ||
'value_select_options', | ||
$this->sourceYesNo->toOptionArray() | ||
); | ||
} | ||
return $this->getData('value_select_options'); | ||
} | ||
|
||
/** | ||
* @return Mobile | ||
*/ | ||
public function loadAttributeOptions() | ||
{ | ||
$this->setData( | ||
'attribute_option', | ||
[ | ||
'is_mobile' => __('Mobile device') | ||
] | ||
); | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* @param Context $context | ||
* @param array $data | ||
* @codeCoverageIgnore | ||
*/ | ||
protected function callParentConstructor(Context $context, array $data): void | ||
{ | ||
parent::__construct($context, $data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* File:MobileRule.php | ||
* | ||
* @author Maciej Sławik <maciej.slawik@lizardmedia.pl> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\CartRuleMobile\Observer; | ||
|
||
use LizardMedia\CartRuleMobile\Model\Rule\Condition\Mobile; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
|
||
/** | ||
* Class MobileRule | ||
* @package LizardMedia\CartRuleMobile\Observer | ||
*/ | ||
class MobileRule implements ObserverInterface | ||
{ | ||
/** | ||
* @param Observer $observer | ||
* @return void | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
$additional = $observer->getData('additional'); | ||
$conditions = array_merge_recursive((array)$additional->getConditions(), [ | ||
$this->getMobileConditionDefinition() | ||
]); | ||
$additional->setConditions($conditions); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private function getMobileConditionDefinition(): array | ||
{ | ||
return [ | ||
'label'=> __('Mobile device'), | ||
'value'=> Mobile::class | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* File:MobileDetectorTest.php | ||
* | ||
* @author Maciej Sławik <maciej.slawik@lizardmedia.pl> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\CartRuleMobile\Test\Unit\Model; | ||
|
||
use LizardMedia\CartRuleMobile\Model\MobileDetector; | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit_Framework_MockObject_MockObject as MockObject; | ||
use Zend_Http_UserAgent; | ||
|
||
/** | ||
* Class MobileDetectorTest | ||
* @package LizardMedia\CartRuleMobile\Test\Unit\Model | ||
*/ | ||
class MobileDetectorTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function testIsMobile() | ||
{ | ||
/** @var MockObject|Zend_Http_UserAgent $userAgent */ | ||
$userAgent = $this->getMockBuilder(Zend_Http_UserAgent::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$mobileDetector = new MobileDetector($userAgent); | ||
|
||
$userAgent->expects($this->once()) | ||
->method('getUserAgent') | ||
->willReturn('android'); | ||
|
||
$this->assertTrue($mobileDetector->isMobileDevice()); | ||
} | ||
} |
Oops, something went wrong.