Skip to content
Projects
Groups
Snippets
Help
Sign in / Register
Toggle navigation
U
unleash-client-php
Project overview
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
0
Merge Requests
2
CI / CD
Security & Compliance
Packages
Wiki
Snippets
Members
Collapse sidebar
Close sidebar
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Minds
unleash-client-php
Commits
ac64d7ca
Commit
ac64d7ca
authored
1 hour ago
by
Emiliano Balbuena
Browse files
Options
Download
(wip): Strategy algorithms
parent
bc1ccc87
goal/clean-up-unleash-client
1 merge request
!2
WIP: Clean up and strategies
Pipeline
#105677760
passed with stage
in 1 minute and 2 seconds
Changes
10
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
353 additions
and
0 deletions
+353
-0
src/Helpers/NormalizedValue.php
0 → 100644
View file @
ac64d7ca
<?php
/**
* NormalizedValue
*
* @author edgebal
*/
namespace
Minds\UnleashClient\Helpers
;
use
lastguest\Murmur
;
class
NormalizedValue
{
/**
* Normalizes a value using Murmur3 algorithm hash and a normalizer modulus.
* Returns a value from 1 to 100 if ID is truthy, if not it returns 0.
* @param string $id
* @param string $groupId
* @param int $normalizer
* @return int
*/
public
static
function
build
(
string
$id
,
string
$groupId
,
int
$normalizer
=
100
)
:
int
{
if
(
!
$id
)
{
return
0
;
}
return
(
Murmur
::
hash3_int
(
"
{
$id
}
:
{
$groupId
}
"
)
%
$normalizer
)
+
1
;
}
}
This diff is collapsed.
src/StrategyAlgorithms/ApplicationHostnameStrategyAlgorithm.php
0 → 100644
View file @
ac64d7ca
<?php
/**
* ApplicationHostnameStrategyAlgorithm
*
* @author edgebal
*/
namespace
Minds\UnleashClient\StrategyAlgorithms
;
use
Minds\UnleashClient\Entities\Context
;
use
Minds\UnleashClient\Entities\Strategy
;
class
ApplicationHostnameStrategyAlgorithm
implements
StrategyAlgorithm
{
/**
* @inheritDoc
*/
public
function
isEnabled
(
Strategy
$strategy
,
Context
$context
)
:
bool
{
$parameters
=
$strategy
->
getParameters
()
??
[];
$hostNamesList
=
$parameters
[
'hostNames'
]
??
''
;
if
(
!
$hostNamesList
)
{
return
false
;
}
$hostNames
=
array_map
([
$this
,
'normalizeHostName'
],
explode
(
','
,
$hostNamesList
));
return
in_array
(
strtolower
(
$context
->
getHostName
()),
$hostNames
,
true
);
}
/**
* Normalizes host names for lookup
* @param string $hostName
* @return string
*/
protected
function
normalizeHostName
(
string
$hostName
)
:
string
{
return
trim
(
strtolower
(
$hostName
));
}
}
This diff is collapsed.
src/StrategyAlgorithms/DefaultStrategyAlgorithm.php
0 → 100644
View file @
ac64d7ca
<?php
/**
* DefaultStrategyAlgorithm
*
* @author edgebal
*/
namespace
Minds\UnleashClient\StrategyAlgorithms
;
use
Minds\UnleashClient\Entities\Context
;
use
Minds\UnleashClient\Entities\Strategy
;
class
DefaultStrategyAlgorithm
implements
StrategyAlgorithm
{
/**
* @inheritDoc
*/
public
function
isEnabled
(
Strategy
$strategy
,
Context
$context
)
:
bool
{
return
true
;
}
}
This diff is collapsed.
src/StrategyAlgorithms/FlexibleRolloutStrategyAlgorithm.php
0 → 100644
View file @
ac64d7ca
<?php
/**
* FlexibleRolloutStrategyAlgorithm
*
* @author edgebal
*/
namespace
Minds\UnleashClient\StrategyAlgorithms
;
use
Minds\UnleashClient\Entities\Context
;
use
Minds\UnleashClient\Entities\Strategy
;
use
Minds\UnleashClient\Helpers\NormalizedValue
;
class
FlexibleRolloutStrategyAlgorithm
implements
StrategyAlgorithm
{
/**
* @inheritDoc
*/
public
function
isEnabled
(
Strategy
$strategy
,
Context
$context
)
:
bool
{
$parameters
=
$strategy
->
getParameters
();
$percentage
=
$parameters
[
'rollout'
]
??
0
;
$stickiness
=
$parameters
[
'stickiness'
]
??
'default'
;
$groupId
=
trim
(
$parameters
[
'groupId'
]
??
''
);
$userId
=
trim
(
$context
->
getUserId
()
??
''
);
$sessionId
=
trim
(
$context
->
getSessionId
()
??
''
);
switch
(
$stickiness
)
{
case
'userId'
:
$stickinessId
=
$userId
;
break
;
case
'sessionId'
:
$stickinessId
=
$sessionId
;
break
;
case
'random'
:
$stickinessId
=
sprintf
(
"%s"
,
mt_rand
(
1
,
100
));
break
;
default
:
$stickinessId
=
$userId
?:
$sessionId
?:
sprintf
(
"%s"
,
mt_rand
(
1
,
100
));
break
;
}
if
(
!
$stickinessId
)
{
return
false
;
}
$stickinessValue
=
NormalizedValue
::
build
(
$stickinessId
,
$groupId
);
return
$percentage
>
0
&&
$stickinessValue
<=
$percentage
;
}
}
This diff is collapsed.
src/StrategyAlgorithms/GradualRolloutRandomStrategyAlgorithm.php
0 → 100644
View file @
ac64d7ca
<?php
/**
* GradualRolloutRandomStrategyAlgorithm
*
* @author edgebal
*/
namespace
Minds\UnleashClient\StrategyAlgorithms
;
use
Minds\UnleashClient\Entities\Context
;
use
Minds\UnleashClient\Entities\Strategy
;
class
GradualRolloutRandomStrategyAlgorithm
implements
StrategyAlgorithm
{
/**
* @inheritDoc
*/
public
function
isEnabled
(
Strategy
$strategy
,
Context
$context
)
:
bool
{
$parameters
=
$strategy
->
getParameters
();
$percentage
=
$parameters
[
'percentage'
]
??
0
;
$random
=
mt_rand
(
1
,
100
);
return
$percentage
>
0
&&
$random
<=
$percentage
;
}
}
This diff is collapsed.
src/StrategyAlgorithms/GradualRolloutSessionIdStrategyAlgorithm.php
0 → 100644
View file @
ac64d7ca
<?php
/**
* GradualRolloutSessionIdStrategyAlgorithm
*
* @author edgebal
*/
namespace
Minds\UnleashClient\StrategyAlgorithms
;
use
Minds\UnleashClient\Entities\Context
;
use
Minds\UnleashClient\Entities\Strategy
;
use
Minds\UnleashClient\Helpers\NormalizedValue
;
class
GradualRolloutSessionIdStrategyAlgorithm
implements
StrategyAlgorithm
{
/**
* @inheritDoc
*/
public
function
isEnabled
(
Strategy
$strategy
,
Context
$context
)
:
bool
{
$parameters
=
$strategy
->
getParameters
();
$percentage
=
$parameters
[
'percentage'
]
??
0
;
$groupId
=
trim
(
$parameters
[
'groupId'
]
??
''
);
$sessionId
=
trim
(
$context
->
getSessionId
()
??
''
);
if
(
!
$sessionId
)
{
return
false
;
}
$sessionIdValue
=
NormalizedValue
::
build
(
$sessionId
,
$groupId
);
return
$percentage
>
0
&&
$sessionIdValue
<=
$percentage
;
}
}
This diff is collapsed.
src/StrategyAlgorithms/GradualRolloutUserIdStrategyAlgorithm.php
0 → 100644
View file @
ac64d7ca
<?php
/**
* GradualRolloutUserIdStrategyAlgorithm
*
* @author edgebal
*/
namespace
Minds\UnleashClient\StrategyAlgorithms
;
use
Minds\UnleashClient\Entities\Context
;
use
Minds\UnleashClient\Entities\Strategy
;
use
Minds\UnleashClient\Helpers\NormalizedValue
;
class
GradualRolloutUserIdStrategyAlgorithm
implements
StrategyAlgorithm
{
/**
* @inheritDoc
*/
public
function
isEnabled
(
Strategy
$strategy
,
Context
$context
)
:
bool
{
$parameters
=
$strategy
->
getParameters
();
$percentage
=
$parameters
[
'percentage'
]
??
0
;
$groupId
=
trim
(
$parameters
[
'groupId'
]
??
''
);
$userId
=
trim
(
$context
->
getUserId
()
??
''
);
if
(
!
$userId
)
{
return
false
;
}
$userIdValue
=
NormalizedValue
::
build
(
$userId
,
$groupId
);
return
$percentage
>
0
&&
$userIdValue
<=
$percentage
;
}
}
This diff is collapsed.
src/StrategyAlgorithms/RemoteAddressStrategyAlgorithm.php
0 → 100644
View file @
ac64d7ca
<?php
/**
* RemoteAddressStrategyAlgorithm
*
* @author edgebal
*/
namespace
Minds\UnleashClient\StrategyAlgorithms
;
use
Minds\UnleashClient\Entities\Context
;
use
Minds\UnleashClient\Entities\Strategy
;
class
RemoteAddressStrategyAlgorithm
implements
StrategyAlgorithm
{
/**
* @inheritDoc
*/
public
function
isEnabled
(
Strategy
$strategy
,
Context
$context
)
:
bool
{
$parameters
=
$strategy
->
getParameters
()
??
[];
$ipAddressesList
=
$parameters
[
'IPs'
]
??
''
;
if
(
!
$ipAddressesList
)
{
return
false
;
}
$ipAddresses
=
array_map
([
$this
,
'normalizeIpAddress'
],
explode
(
','
,
$ipAddressesList
));
return
in_array
(
strtolower
(
$context
->
getRemoteAddress
()),
$ipAddresses
,
true
);
}
/**
* Normalizes IP addresses for lookup, using lowercase for IPv6
* @param string $ipAddress
* @return string
*/
protected
function
normalizeIpAddress
(
string
$ipAddress
)
:
string
{
return
trim
(
strtolower
(
$ipAddress
));
}
}
This diff is collapsed.
src/StrategyAlgorithms/StrategyAlgorithm.php
0 → 100644
View file @
ac64d7ca
<?php
/**
* StrategyAlgorithm
*
* @author edgebal
*/
namespace
Minds\UnleashClient\StrategyAlgorithms
;
use
Minds\UnleashClient\Entities\Context
;
use
Minds\UnleashClient\Entities\Strategy
;
interface
StrategyAlgorithm
{
/**
* Resolves a strategy using the context
* @param Strategy $strategy
* @param Context $context
* @return bool
*/
public
function
isEnabled
(
Strategy
$strategy
,
Context
$context
)
:
bool
;
}
This diff is collapsed.
src/StrategyAlgorithms/UserWithIdStrategyAlgorithm.php
0 → 100644
View file @
ac64d7ca
<?php
/**
* UserWithIdStrategyAlgorithm
*
* @author edgebal
*/
namespace
Minds\UnleashClient\StrategyAlgorithms
;
use
Minds\UnleashClient\Entities\Context
;
use
Minds\UnleashClient\Entities\Strategy
;
class
UserWithIdStrategyAlgorithm
implements
StrategyAlgorithm
{
/**
* @inheritDoc
*/
public
function
isEnabled
(
Strategy
$strategy
,
Context
$context
)
:
bool
{
$parameters
=
$strategy
->
getParameters
()
??
[];
$userIdsList
=
$parameters
[
'userIds'
]
??
''
;
if
(
!
$userIdsList
)
{
return
false
;
}
$userIds
=
array_map
([
$this
,
'normalizeUserId'
],
explode
(
','
,
$userIdsList
));
return
in_array
(
$context
->
getUserId
(),
$userIds
,
true
);
}
/**
* Normalizes user IDs for lookup
* @param string $userId
* @return string
*/
protected
function
normalizeUserId
(
string
$userId
)
:
string
{
return
trim
(
$userId
);
}
}
This diff is collapsed.
Please
register
or
sign in
to comment