Skip to content

Commit

Permalink
Added segment information into sales funnels
Browse files Browse the repository at this point in the history
remp/novydenik#1228
  • Loading branch information
burithetech committed Mar 25, 2024
1 parent 7d21105 commit a6c4850
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 24 deletions.
20 changes: 20 additions & 0 deletions src/Models/Config/SegmentSlowRecalculateThreshold.php
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 src/Models/Config/SegmentSlowRecalculateThresholdFactory.php
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);
}
}
}
26 changes: 5 additions & 21 deletions src/Presenters/StoredSegmentsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Crm\ApplicationModule\Models\Widget\WidgetManager;
use Crm\SegmentModule\Forms\SegmentFormFactory;
use Crm\SegmentModule\Forms\SegmentRecalculationSettingsFormFactory;
use Crm\SegmentModule\Models\Config\SegmentSlowRecalculateThresholdFactory;
use Crm\SegmentModule\Models\SegmentException;
use Crm\SegmentModule\Models\SegmentFactoryInterface;
use Crm\SegmentModule\Models\SegmentWidgetInterface;
Expand Down Expand Up @@ -60,7 +61,8 @@ public function __construct(
SegmentGroupsRepository $segmentGroupsRepository,
AccessToken $accessToken,
WidgetManager $widgetManager,
LazyWidgetManager $lazyWidgetManager
LazyWidgetManager $lazyWidgetManager,
private SegmentSlowRecalculateThresholdFactory $segmentSlowRecalculateThresholdFactory,
) {
parent::__construct();

Expand All @@ -83,16 +85,7 @@ public function renderDefault()
$this->template->segmentGroups = $this->segmentGroupsRepository->all();
$this->template->segments = $this->segmentsRepository->all();
$this->template->deletedSegments = $this->segmentsRepository->deleted();

$segmentSlowRecalculateThreshold = $this->applicationConfig->get('segment_slow_recalculate_threshold');
if (!is_numeric($segmentSlowRecalculateThreshold) || $segmentSlowRecalculateThreshold < 0) {
Debugger::log(
'Bad value in configuration for `segment_slow_recalculate_threshold`. Value must be positive number.',
Debugger::WARNING
);
} else {
$this->template->segmentSlowRecalculateThreshold = $this->applicationConfig->get('segment_slow_recalculate_threshold');
}
$this->template->segmentSlowRecalculateThresholdInSeconds = $this->segmentSlowRecalculateThresholdFactory->build()->thresholdInSeconds;
}

/**
Expand Down Expand Up @@ -124,16 +117,7 @@ public function renderShow($id, $data = false)
$segmentRow = $this->loadSegment($id);
$this->template->segment = $segmentRow;
$this->template->showData = $data;

$segmentSlowRecalculateThreshold = $this->applicationConfig->get('segment_slow_recalculate_threshold');
if (!is_numeric($segmentSlowRecalculateThreshold) || $segmentSlowRecalculateThreshold < 0) {
Debugger::log(
'Bad value in configuration for `segment_slow_recalculate_threshold`. Value must be positive number.',
Debugger::WARNING
);
} else {
$this->template->segmentSlowRecalculateThreshold = $this->applicationConfig->get('segment_slow_recalculate_threshold');
}
$this->template->segmentSlowRecalculateThresholdInSeconds = $this->segmentSlowRecalculateThresholdFactory->build()->thresholdInSeconds;

$segment = $this->segmentFactory->buildSegment($segmentRow->code);

Expand Down
47 changes: 47 additions & 0 deletions src/Tests/Config/SegmentSlowRecalculateThresholdFactoryTest.php
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);
}
}
31 changes: 31 additions & 0 deletions src/Tests/Config/SegmentSlowRecalculateThresholdTest.php
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);
}
}
3 changes: 2 additions & 1 deletion src/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ services:
- Crm\SegmentModule\Seeders\SegmentsSeeder
- Crm\SegmentModule\Seeders\ConfigSeeder
- Crm\SegmentModule\Segment\SegmentCriteria
- Crm\SegmentModule\Events\BeforeSegmentCodeUpdateHandler
- Crm\SegmentModule\Events\BeforeSegmentCodeUpdateHandler
- Crm\SegmentModule\Models\Config\SegmentSlowRecalculateThresholdFactory
2 changes: 1 addition & 1 deletion src/templates/StoredSegments/default.latte
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<td>~{$segment->cache_count}</td>
<td>
{if isset($segment->cache_count_time)}
<span {if isset($segmentSlowRecalculateThreshold) && $segment->cache_count_time > $segmentSlowRecalculateThreshold}style="color: red;"{/if}>
<span {if $segmentSlowRecalculateThresholdInSeconds !== null && $segment->cache_count_time > $segmentSlowRecalculateThresholdInSeconds}style="color: red;"{/if}>
{$segment->cache_count_time}&nbsp;s
</span>
{/if}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/StoredSegments/show.latte
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
{/if}
{if $segment->cache_count_time}
<span {if isset($segmentSlowRecalculateThreshold) && $segment->cache_count_time > $segmentSlowRecalculateThreshold}style="color: red;"{/if}>
<span {if $segmentSlowRecalculateThresholdInSeconds !== null && $segment->cache_count_time > $segmentSlowRecalculateThresholdInSeconds}style="color: red;"{/if}>
{_segment.fields.time}: {$segment->cache_count_time}&nbsp;s
</span>
{/if})
Expand Down

0 comments on commit a6c4850

Please sign in to comment.