-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added segment information into sales funnels
remp/novydenik#1228
- Loading branch information
1 parent
7d21105
commit a6c4850
Showing
8 changed files
with
146 additions
and
24 deletions.
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,20 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Crm\SegmentModule\Models\Config; | ||
|
||
use DomainException; | ||
|
||
class SegmentSlowRecalculateThreshold | ||
{ | ||
public function __construct(public readonly ?int $thresholdInSeconds) | ||
{ | ||
if ($thresholdInSeconds === null) { | ||
return; | ||
} | ||
|
||
if ($thresholdInSeconds < 0) { | ||
throw new DomainException('Threshold must be a positive number.'); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Models/Config/SegmentSlowRecalculateThresholdFactory.php
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,39 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Crm\SegmentModule\Models\Config; | ||
|
||
use Crm\ApplicationModule\Models\Config\ApplicationConfig; | ||
use Exception; | ||
use Tracy\Debugger; | ||
|
||
class SegmentSlowRecalculateThresholdFactory | ||
{ | ||
private const SLOW_RECALCULATE_THRESHOLD_CONFIG_KEY = 'segment_slow_recalculate_threshold'; | ||
|
||
public function __construct( | ||
private readonly ApplicationConfig $applicationConfig, | ||
) { | ||
} | ||
|
||
public function build(): SegmentSlowRecalculateThreshold | ||
{ | ||
try { | ||
$threshold = $this->applicationConfig->get(self::SLOW_RECALCULATE_THRESHOLD_CONFIG_KEY); | ||
if (!is_numeric($threshold)) { | ||
throw new Exception('Threshold must be a number.'); | ||
} | ||
|
||
return new SegmentSlowRecalculateThreshold($threshold); | ||
} catch (Exception $exception) { | ||
$errorMessage = sprintf( | ||
'Bad value in configuration for `%s`. %s', | ||
self::SLOW_RECALCULATE_THRESHOLD_CONFIG_KEY, | ||
$exception->getMessage() | ||
); | ||
Debugger::log($errorMessage, Debugger::WARNING); | ||
|
||
return new SegmentSlowRecalculateThreshold(null); | ||
} | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
src/Tests/Config/SegmentSlowRecalculateThresholdFactoryTest.php
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,47 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Crm\SegmentModule\Tests\Config; | ||
|
||
use Crm\ApplicationModule\Models\Config\ApplicationConfig; | ||
use Crm\SegmentModule\Models\Config\SegmentSlowRecalculateThresholdFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SegmentSlowRecalculateThresholdFactoryTest extends TestCase | ||
{ | ||
public function testBuild(): void | ||
{ | ||
$mockedApplicationConfig = $this->createMock(ApplicationConfig::class); | ||
$mockedApplicationConfig->expects($this->once()) | ||
->method('get') | ||
->with('segment_slow_recalculate_threshold') | ||
->willReturn(10); | ||
|
||
$factory = new SegmentSlowRecalculateThresholdFactory($mockedApplicationConfig); | ||
$this->assertSame(10, $factory->build()->thresholdInSeconds); | ||
} | ||
|
||
public function testBuildWithDomainException(): void | ||
{ | ||
$mockedApplicationConfig = $this->createMock(ApplicationConfig::class); | ||
$mockedApplicationConfig->expects($this->once()) | ||
->method('get') | ||
->with('segment_slow_recalculate_threshold') | ||
->willReturn(-1); | ||
|
||
$factory = new SegmentSlowRecalculateThresholdFactory($mockedApplicationConfig); | ||
$this->assertNull($factory->build()->thresholdInSeconds); | ||
} | ||
|
||
public function testBuildWithException(): void | ||
{ | ||
$mockedApplicationConfig = $this->createMock(ApplicationConfig::class); | ||
$mockedApplicationConfig->expects($this->once()) | ||
->method('get') | ||
->with('segment_slow_recalculate_threshold') | ||
->willReturn('not a valid value'); | ||
|
||
$factory = new SegmentSlowRecalculateThresholdFactory($mockedApplicationConfig); | ||
$this->assertNull($factory->build()->thresholdInSeconds); | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Crm\SegmentModule\Tests\Config; | ||
|
||
use Crm\SegmentModule\Models\Config\SegmentSlowRecalculateThreshold; | ||
use DomainException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SegmentSlowRecalculateThresholdTest extends TestCase | ||
{ | ||
public function testThreshold(): void | ||
{ | ||
$threshold = new SegmentSlowRecalculateThreshold(10); | ||
$this->assertSame(10, $threshold->thresholdInSeconds); | ||
} | ||
|
||
public function testNegativeThreshold(): void | ||
{ | ||
$this->expectException(DomainException::class); | ||
$this->expectExceptionMessage('Threshold must be a positive number.'); | ||
|
||
new SegmentSlowRecalculateThreshold(-1); | ||
} | ||
|
||
public function testNotSetThreshold(): void | ||
{ | ||
$threshold = new SegmentSlowRecalculateThreshold(null); | ||
$this->assertNull($threshold->thresholdInSeconds); | ||
} | ||
} |
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
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