ver. 0.288: BasketCalculator + ShopBasketController migration, cms\Layout removal

- Migrate front\factory\ShopBasket → Domain\Basket\BasketCalculator (4 static methods, 18 callers updated)
- Migrate front\controls\ShopBasket → front\Controllers\ShopBasketController (camelCase, instance methods)
- Add snake_case→camelCase action dispatch for new controllers in Site::route()
- Update title()/page_title() to check front\Controllers\ before fallback
- Remove cms\Layout class (replaced by $layoutsRepo->find())
- Add 8 tests for BasketCalculator (484 tests, 1528 assertions)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-17 09:38:45 +01:00
parent a7e44f23fa
commit 25348797da
22 changed files with 287 additions and 275 deletions

View File

@@ -0,0 +1,62 @@
<?php
namespace Domain\Basket;
class BasketCalculator
{
public static function summaryWp($basket)
{
$wp = 0;
if (is_array($basket)) {
foreach ($basket as $product) {
$wp += $product['wp'] * $product['quantity'];
}
}
return $wp;
}
public static function countProductsText($count)
{
$count = (int)$count;
if ($count === 1) {
return $count . ' produkt';
}
if ($count >= 2 && $count <= 4) {
return $count . ' produkty';
}
return $count . ' produktów';
}
public static function summaryPrice($basket, $coupon = null)
{
global $lang_id;
$summary = 0;
if (is_array($basket)) {
foreach ($basket as $position) {
$product = \shop\Product::getFromCache((int)$position['product-id'], $lang_id);
$product_price_tmp = \shop\Product::calculate_basket_product_price(
(float)$product['price_brutto_promo'],
(float)$product['price_brutto'],
$coupon,
$position
);
$summary += $product_price_tmp['price_new'] * $position['quantity'];
}
}
return \Shared\Helpers\Helpers::normalize_decimal($summary);
}
public static function countProducts($basket)
{
$count = 0;
if (is_array($basket)) {
foreach ($basket as $product) {
$count += $product['quantity'];
}
}
return $count;
}
}

View File

@@ -1,44 +0,0 @@
<?
namespace cms;
class Layout implements \ArrayAccess
{
public function __construct( int $layout_id )
{
global $mdb;
$result = $mdb -> get( 'pp_layouts', '*', [ 'id' => $layout_id ] );
if ( \Shared\Helpers\Helpers::is_array_fix( $result ) ) foreach ( $result as $key => $val )
$this -> $key = $val;
}
public function __get( $variable )
{
if ( array_key_exists( $variable, $this -> data ) )
return $this -> $variable;
}
public function __set( $variable, $value )
{
$this -> $variable = $value;
}
public function offsetExists( $offset )
{
return isset( $this -> $offset );
}
public function offsetGet( $offset )
{
return $this -> $offset;
}
public function offsetSet( $offset, $value )
{
$this -> $offset = $value;
}
public function offsetUnset( $offset )
{
unset( $this -> $offset );
}
}

View File

@@ -1,22 +1,20 @@
<?php
namespace front\controls;
namespace front\Controllers;
class ShopBasket
class ShopBasketController
{
public static $title = [
'main_view' => 'Koszyk'
'mainView' => 'Koszyk'
];
public static function basket_message_save()
public function basketMessageSave()
{
\Shared\Helpers\Helpers::set_session( 'basket_message', \Shared\Helpers\Helpers::get( 'basket_message' ) );
echo json_encode( [
'result' => 'ok'
] );
echo json_encode( [ 'result' => 'ok' ] );
exit;
}
public static function basket_remove_product()
public function basketRemoveProduct()
{
global $lang_id;
@@ -31,24 +29,10 @@ class ShopBasket
\Shared\Helpers\Helpers::set_session( 'basket', $basket );
echo json_encode( [
'basket' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-details', [
'basket' => $basket,
'lang_id' => $lang_id,
'coupon' => $coupon
] ),
'basket_mini_count' => \front\factory\ShopBasket::count_products_text( \front\factory\ShopBasket::count_products( $basket ) ),
'basket_mini_value' => \front\factory\ShopBasket::summary_price( $basket, $coupon ),
'products_count' => count( $basket ),
'transport_methods' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-transport-methods', [
'transports_methods' => \front\factory\ShopTransport::transport_methods( $basket, $coupon ),
'transport_id' => $basket_transport_method_id
] )
] );
exit;
$this->jsonBasketResponse( $basket, $coupon, $lang_id, $basket_transport_method_id );
}
public static function basket_increase_quantity_product()
public function basketIncreaseQuantityProduct()
{
global $lang_id;
@@ -66,25 +50,10 @@ class ShopBasket
\Shared\Helpers\Helpers::set_session( 'basket', $basket );
echo json_encode( [
'basket' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-details', [
'basket' => $basket,
'lang_id' => $lang_id,
'coupon' => $coupon
] ),
'basket_mini_count' => \front\factory\ShopBasket::count_products_text( \front\factory\ShopBasket::count_products( $basket ) ),
'basket_mini_value' => \front\factory\ShopBasket::summary_price( $basket, $coupon ),
'products_count' => count( $basket ),
'transport_methods' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-transport-methods', [
'transports_methods' => \front\factory\ShopTransport::transport_methods( $basket, $coupon ),
'transport_id' => $basket_transport_method_id
] )
]
);
exit;
$this->jsonBasketResponse( $basket, $coupon, $lang_id, $basket_transport_method_id );
}
public static function basket_decrease_quantity_product()
public function basketDecreaseQuantityProduct()
{
global $lang_id;
@@ -102,24 +71,10 @@ class ShopBasket
\Shared\Helpers\Helpers::set_session( 'basket', $basket );
echo json_encode( [
'basket' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-details', [
'basket' => $basket,
'lang_id' => $lang_id,
'coupon' => $coupon
] ),
'basket_mini_count' => \front\factory\ShopBasket::count_products_text( \front\factory\ShopBasket::count_products( $basket ) ),
'basket_mini_value' => \front\factory\ShopBasket::summary_price( $basket, $coupon ),
'products_count' => count( $basket ),
'transport_methods' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-transport-methods', [
'transports_methods' => \front\factory\ShopTransport::transport_methods( $basket, $coupon ),
'transport_id' => $basket_transport_method_id
] )
] );
exit;
$this->jsonBasketResponse( $basket, $coupon, $lang_id, $basket_transport_method_id );
}
public static function basket_change_quantity_product()
public function basketChangeQuantityProduct()
{
global $lang_id;
@@ -139,24 +94,10 @@ class ShopBasket
$basket = \Shared\Helpers\Helpers::get_session( 'basket' );
echo json_encode( [
'basket' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-details', [
'basket' => $basket,
'lang_id' => $lang_id,
'coupon' => $coupon
] ),
'basket_mini_count' => \front\factory\ShopBasket::count_products_text( \front\factory\ShopBasket::count_products( $basket ) ),
'basket_mini_value' => \front\factory\ShopBasket::summary_price( $basket, $coupon ),
'products_count' => count( $basket ),
'transport_methods' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-transport-methods', [
'transports_methods' => \front\factory\ShopTransport::transport_methods( $basket, $coupon ),
'transport_id' => $basket_transport_method_id
] )
] );
exit;
$this->jsonBasketResponse( $basket, $coupon, $lang_id, $basket_transport_method_id );
}
static public function product_message_change()
public function productMessageChange()
{
$basket = \Shared\Helpers\Helpers::get_session( 'basket' );
$basket[ \Shared\Helpers\Helpers::get( 'position_code' ) ]['message'] = \Shared\Helpers\Helpers::get( 'product_message' );
@@ -164,30 +105,29 @@ class ShopBasket
exit;
}
public static function basket_add_product()
public function basketAddProduct()
{
$basket = \shop\Basket::validate_basket( \Shared\Helpers\Helpers::get_session( 'basket' ) );
$values_tmp = json_decode( \Shared\Helpers\Helpers::get( 'values' ), true );
$values = [];
$attributes = [];
$custom_fields = [];
foreach( $values_tmp as $key => $val )
$values[ $val['name'] ] = $val['value'];
// sprawdzam pola pod kątem wybranych atrybutów
foreach( $values as $key => $val )
{
if ( $key != 'product-id' and $key != 'quantity' and $key != 'product-message' and strpos( $key, 'custom_field' ) === false )
$attributes[] = $val;
}
// stwórz tablicę dodatkowych pól wyszukując na podstawie custom_field[1], custom_field[2] itd.
foreach( $values as $key => $val )
{
if ( strpos( $key, 'custom_field' ) !== false )
{
// extract number from custom_field[1], custom_field[2] etc.
preg_match( '/\d+/', $key, $matches );
$custom_field_id = $matches[0];
$custom_fields[ $custom_field_id ] = $val;
}
}
@@ -199,12 +139,10 @@ class ShopBasket
$values['attributes'] = $attributes;
}
$values['wp'] = \front\factory\ShopProduct::product_wp( $values[ 'product-id' ] );
$attributes_implode = '';
// generuj unikalny kod produktu dodanego do koszyka
if ( is_array( $attributes ) )
if ( is_array( $attributes ) and count( $attributes ) > 0 )
$attributes_implode = implode( '|', $attributes );
$product_code = md5( $values['product-id'] . $attributes_implode . $values['product-message'] . json_encode( $custom_fields ) );
@@ -225,95 +163,78 @@ class ShopBasket
echo json_encode( [
'result' => 'ok',
'basket_mini_count' => \front\factory\ShopBasket::count_products_text( \front\factory\ShopBasket::count_products( $basket ) ),
'basket_mini_value' => \front\factory\ShopBasket::summary_price( $basket, $coupon ),
'basket_mini_count' => \Domain\Basket\BasketCalculator::countProductsText( \Domain\Basket\BasketCalculator::countProducts( $basket ) ),
'basket_mini_value' => \Domain\Basket\BasketCalculator::summaryPrice( $basket, $coupon ),
'product_sets' => \shop\Product::product_sets_when_add_to_basket( (int)$values['product-id'] )
] );
exit;
}
// sprawdzam czy została wybrana forma wysylki inpost i czy został wybrany paczkomat
static public function transport_method_inpost_check()
public function transportMethodInpostCheck()
{
if ( \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' ) === '2' or \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' ) === '1' )
$transport_id = \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' );
if ( $transport_id === '2' or $transport_id === '1' )
{
if ( !\Shared\Helpers\Helpers::get_session( 'basket-inpost-info' ) )
{
echo json_encode( [
'result' => 'bad'
] );
echo json_encode( [ 'result' => 'bad' ] );
exit;
}
}
if ( \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' ) === '9' )
if ( $transport_id === '9' )
{
if ( !\Shared\Helpers\Helpers::get_session( 'basket_orlen_point_id' ) )
{
echo json_encode( [
'result' => 'bad'
] );
echo json_encode( [ 'result' => 'bad' ] );
exit;
}
}
echo json_encode( [
'result' => 'ok'
] );
echo json_encode( [ 'result' => 'ok' ] );
exit;
}
// sprawdzam czy został wybrany paczkomat
static public function inpost_check() {
public function inpostCheck()
{
if ( !\Shared\Helpers\Helpers::get_session( 'basket-inpost-info' ) )
echo json_encode( [
'result' => 'bad'
] );
echo json_encode( [ 'result' => 'bad' ] );
else
echo json_encode( [
'result' => 'ok'
] );
echo json_encode( [ 'result' => 'ok' ] );
exit;
}
static public function orlen_save()
public function orlenSave()
{
\Shared\Helpers\Helpers::set_session( 'basket_orlen_point_id', \Shared\Helpers\Helpers::get( 'orlen_point_id' ) );
\Shared\Helpers\Helpers::set_session( 'basket_orlen_point_info', \Shared\Helpers\Helpers::get( 'orlen_point_name' ) );
echo json_encode( [
'result' => 'ok'
] );
echo json_encode( [ 'result' => 'ok' ] );
exit;
}
public static function inpost_save()
public function inpostSave()
{
\Shared\Helpers\Helpers::set_session( 'basket-inpost-info', \Shared\Helpers\Helpers::get( 'paczkomat' ) );
echo json_encode( [
'result' => 'ok'
] );
echo json_encode( [ 'result' => 'ok' ] );
exit;
}
public static function basket_payment_method_set()
public function basketPaymentMethodSet()
{
\Shared\Helpers\Helpers::set_session( 'basket-payment-method-id', \Shared\Helpers\Helpers::get( 'payment_method_id' ) );
echo json_encode( [
'result' => 'ok'
] );
echo json_encode( [ 'result' => 'ok' ] );
exit;
}
public static function basket_transport_method_set()
public function basketTransportMethodSet()
{
\Shared\Helpers\Helpers::set_session( 'basket-transport-method-id', \Shared\Helpers\Helpers::get( 'transport_method_id' ) );
echo json_encode( [
'result' => 'ok'
] );
echo json_encode( [ 'result' => 'ok' ] );
exit;
}
public static function basket_payments_methods()
public function basketPaymentsMethods()
{
\Shared\Helpers\Helpers::set_session( 'basket-transport-method-id', \Shared\Helpers\Helpers::get( 'transport_method_id' ) );
@@ -327,7 +248,7 @@ class ShopBasket
exit;
}
public static function summary_view()
public function summaryView()
{
global $lang_id, $settings;
@@ -352,11 +273,9 @@ class ShopBasket
] );
}
// zapisanie koszyka jako zamówienie
static public function basket_save()
public function basketSave()
{
$client = \Shared\Helpers\Helpers::get_session( 'client' );
$payment_method = \Shared\Helpers\Helpers::get_session( 'basket-payment-method-id' );
if ( \shop\Basket::check_product_quantity_in_stock( \Shared\Helpers\Helpers::get_session( 'basket' ) ) )
{
@@ -418,7 +337,7 @@ class ShopBasket
}
}
public static function main_view()
public function mainView()
{
global $lang_id, $page, $settings;
@@ -456,4 +375,22 @@ class ShopBasket
] );
}
}
private function jsonBasketResponse( $basket, $coupon, $lang_id, $basket_transport_method_id )
{
echo json_encode( [
'basket' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-details', [
'basket' => $basket,
'lang_id' => $lang_id,
'coupon' => $coupon
] ),
'basket_mini_count' => \Domain\Basket\BasketCalculator::countProductsText( \Domain\Basket\BasketCalculator::countProducts( $basket ) ),
'basket_mini_value' => \Domain\Basket\BasketCalculator::summaryPrice( $basket, $coupon ),
'products_count' => count( $basket ),
'transport_methods' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-transport-methods', [
'transports_methods' => \front\factory\ShopTransport::transport_methods( $basket, $coupon ),
'transport_id' => $basket_transport_method_id
] )
] );
exit;
}
}

View File

@@ -5,30 +5,34 @@ class Site
{
static public function page_title()
{
$class = '\front\controls\\';
$moduleName = implode( '', array_map( 'ucfirst', explode( '_', \Shared\Helpers\Helpers::get( 'module' ) ) ) );
$action = \Shared\Helpers\Helpers::get( 'action' );
$actionCamel = lcfirst( implode( '', array_map( 'ucfirst', explode( '_', $action ) ) ) );
$results = explode( '_', \Shared\Helpers\Helpers::get( 'module' ) );
if ( is_array( $results ) ) foreach ( $results as $row )
$class .= ucfirst( $row );
$controllerClass = '\front\Controllers\\' . $moduleName . 'Controller';
if ( class_exists( $controllerClass ) and property_exists( $controllerClass, 'title' ) and isset( $controllerClass::$title[$actionCamel] ) )
return $controllerClass::$title[$actionCamel];
$property = \Shared\Helpers\Helpers::get( 'action' );
$class = '\front\controls\\' . $moduleName;
if ( class_exists( $class ) and property_exists( new $class, 'page_title' ) )
return $class::$title[$property];
return $class::$title[$action];
}
static public function title()
{
global $settings;
$class = '\front\controls\\';
$moduleName = implode( '', array_map( 'ucfirst', explode( '_', \Shared\Helpers\Helpers::get( 'module' ) ) ) );
$action = \Shared\Helpers\Helpers::get( 'action' );
$actionCamel = lcfirst( implode( '', array_map( 'ucfirst', explode( '_', $action ) ) ) );
$results = explode( '_', \Shared\Helpers\Helpers::get( 'module' ) );
if ( is_array( $results ) ) foreach ( $results as $row )
$class .= ucfirst( $row );
$controllerClass = '\front\Controllers\\' . $moduleName . 'Controller';
if ( class_exists( $controllerClass ) and property_exists( $controllerClass, 'title' ) and isset( $controllerClass::$title[$actionCamel] ) )
return $controllerClass::$title[$actionCamel] . ' | ' . $settings['firm_name'];
$property = \Shared\Helpers\Helpers::get( 'action' );
$class = '\front\controls\\' . $moduleName;
if ( class_exists( $class ) and property_exists( new $class, 'title' ) )
return $class::$title[$property] . ' | ' . $settings['firm_name'];
return $class::$title[$action] . ' | ' . $settings['firm_name'];
}
public static function route( $product = '', $category = '' )
@@ -64,8 +68,9 @@ class Site
if ( isset( $controllerFactories[$moduleName] ) and $action )
{
$controller = $controllerFactories[$moduleName]();
if ( method_exists( $controller, $action ) )
return $controller->$action();
$actionCamel = lcfirst( implode( '', array_map( 'ucfirst', explode( '_', $action ) ) ) );
if ( method_exists( $controller, $actionCamel ) )
return $controller->$actionCamel();
}
// stare klasy
@@ -162,6 +167,9 @@ class Site
new \Domain\Newsletter\NewsletterRepository( $mdb )
);
},
'ShopBasket' => function() {
return new \front\Controllers\ShopBasketController();
},
];
}
}

View File

@@ -1,66 +0,0 @@
<?php
namespace front\factory;
class ShopBasket
{
public static function summary_wp( $basket )
{
global $mdb;
foreach ( $basket as $product )
{
$wp += $product[ 'wp' ] * $product[ 'quantity' ];
}
return $wp;
}
public static function count_products_text( $count )
{
$count_products = $count;
switch ( true )
{
case ( $count == 0 ): $count_products .= ' produktów';
break;
case ( $count == 1 ): $count_products .= ' produkt';
break;
case ( $count == 2 or $count == 3 or $count == 4 ): $count_products .= ' produkty';
break;
case ( $count >= 5 ): $count_products .= ' produktów';
break;
}
return $count_products;
}
public static function summary_price( $basket, $coupon = null )
{
global $lang_id;
$summary = 0;
if ( is_array( $basket ) )
{
foreach ( $basket as $position )
{
$product = \shop\Product::getFromCache( (int)$position['product-id'], $lang_id );
$product_price_tmp = \shop\Product::calculate_basket_product_price( (float)$product['price_brutto_promo'], (float)$product['price_brutto'], $coupon, $position );
$summary += $product_price_tmp['price_new'] * $position[ 'quantity' ];
}
}
return \Shared\Helpers\Helpers::normalize_decimal( $summary );
}
public static function count_products( $basket )
{
$count = 0;
if ( is_array( $basket ) )
foreach ( $basket as $product )
$count += $product[ 'quantity' ];
return $count;
}
}

View File

@@ -97,7 +97,7 @@ class ShopOrder
$transport = \front\factory\ShopTransport::transport( $transport_id );
$payment_method = \front\factory\ShopPaymentMethod::payment_method( $payment_id );
$basket_summary = \front\factory\ShopBasket::summary_price( $basket, $coupon );
$basket_summary = \Domain\Basket\BasketCalculator::summaryPrice( $basket, $coupon );
$order_number = self::generate_order_number();
$order_date = date( 'Y-m-d H:i:s' );
$hash = md5( $order_number . time() );

View File

@@ -30,7 +30,7 @@ class ShopTransport
$transports_tmp = unserialize( $objectData );
}
$wp_summary = \front\factory\ShopBasket::summary_wp( $basket );
$wp_summary = \Domain\Basket\BasketCalculator::summaryWp( $basket );
foreach ( $transports_tmp as $tr )
{
@@ -41,7 +41,7 @@ class ShopTransport
}
if ( \Shared\Helpers\Helpers::normalize_decimal( \front\factory\ShopBasket::summary_price( $basket, $coupon ) ) >= \Shared\Helpers\Helpers::normalize_decimal( $settings['free_delivery'] ) )
if ( \Shared\Helpers\Helpers::normalize_decimal( \Domain\Basket\BasketCalculator::summaryPrice( $basket, $coupon ) ) >= \Shared\Helpers\Helpers::normalize_decimal( $settings['free_delivery'] ) )
{
for ( $i = 0; $i < count( $transports ); $i++ ){
if($transports[ $i ]['delivery_free'] == 1) {

View File

@@ -29,7 +29,7 @@ class Site
$scontainersRepo = new \Domain\Scontainers\ScontainersRepository( $GLOBALS['mdb'] );
if ( (int) \Shared\Helpers\Helpers::get( 'layout_id' ) )
$layout = new \cms\Layout( (int) \Shared\Helpers\Helpers::get( 'layout_id' ) );
$layout = $layoutsRepo->find( (int) \Shared\Helpers\Helpers::get( 'layout_id' ) );
if ( \Shared\Helpers\Helpers::get( 'article' ) )
$layout = $layoutsRepo->getArticleLayout( (int) \Shared\Helpers\Helpers::get( 'article' ) );