woocommerce-pos
/
vendor_prefixed
/
sentry
/
sentry
/
src
/
DataCollection
/
DataCollectionPolicy.php
DataCollectionPolicy.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.26, at vendor_prefixed/sentry/sentry/src/DataCollection/DataCollectionPolicy.php
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace WCPOS\Vendor\Sentry\DataCollection; |
| 5 | |
| 6 | use WCPOS\Vendor\Sentry\Options; |
| 7 | use WCPOS\Vendor\Sentry\State\HubInterface; |
| 8 | /** |
| 9 | * The policy acts as a container to deal with legacy and new data collection options. |
| 10 | * Checking both options is often tedious and very repetitive and will also make removal |
| 11 | * more difficult in the future. |
| 12 | * |
| 13 | * This class is meant to make it simpler to deal with both competing values and should make |
| 14 | * retiring the legacy option easier |
| 15 | */ |
| 16 | final class DataCollectionPolicy |
| 17 | { |
| 18 | public const MAX_HTTP_BODY_LENGTH = 100000; |
| 19 | private const MAX_REQUEST_BODY_LENGTH = ['none' => 0, 'never' => 0, 'small' => 1000, 'medium' => 10000, 'always' => self::MAX_HTTP_BODY_LENGTH]; |
| 20 | /** |
| 21 | * @var Options|null |
| 22 | */ |
| 23 | private $options; |
| 24 | private function __construct(?Options $options) |
| 25 | { |
| 26 | $this->options = $options; |
| 27 | } |
| 28 | public static function fromHub(HubInterface $hub) : self |
| 29 | { |
| 30 | $client = $hub->getClient(); |
| 31 | return new self($client === null ? null : $client->getOptions()); |
| 32 | } |
| 33 | public static function fromOptions(?Options $options) : self |
| 34 | { |
| 35 | return new self($options); |
| 36 | } |
| 37 | public function getOptions() : ?Options |
| 38 | { |
| 39 | return $this->options; |
| 40 | } |
| 41 | public function getDataCollection() : ?DataCollectionOptions |
| 42 | { |
| 43 | return $this->options === null ? null : $this->options->getDataCollection(); |
| 44 | } |
| 45 | public function isLegacyMode() : bool |
| 46 | { |
| 47 | return $this->getDataCollection() === null; |
| 48 | } |
| 49 | public function getHttpBodyLimit(HttpMessageType $messageType) : ?int |
| 50 | { |
| 51 | $collection = $this->getDataCollection(); |
| 52 | if ($collection === null || !$collection->shouldCollectHttpBody($messageType)) { |
| 53 | return null; |
| 54 | } |
| 55 | return $this->calculateMaxHttpBodyLength($messageType) ?: null; |
| 56 | } |
| 57 | public function getLegacyRequestBodyLimit() : ?int |
| 58 | { |
| 59 | if (!$this->isLegacyMode()) { |
| 60 | return null; |
| 61 | } |
| 62 | return $this->calculateMaxHttpBodyLength(HttpMessageType::incomingRequest()) ?: null; |
| 63 | } |
| 64 | public function shouldCollectUserInfo() : bool |
| 65 | { |
| 66 | $dataCollection = $this->getDataCollection(); |
| 67 | if ($dataCollection !== null) { |
| 68 | return $dataCollection->shouldCollectUserInfo(); |
| 69 | } |
| 70 | return $this->options !== null && $this->options->shouldSendDefaultPii(); |
| 71 | } |
| 72 | /** |
| 73 | * Returns the number of source code lines to include above and below each stack frame, |
| 74 | * or `null` if no source code context should be collected. |
| 75 | */ |
| 76 | public function getFrameContextLines() : ?int |
| 77 | { |
| 78 | $dataCollection = $this->getDataCollection(); |
| 79 | if ($dataCollection !== null) { |
| 80 | return $dataCollection->getFrameContextLines(); |
| 81 | } |
| 82 | return $this->options === null ? null : $this->options->getContextLines(); |
| 83 | } |
| 84 | public function shouldCollectDatabaseQueryData() : bool |
| 85 | { |
| 86 | $dataCollection = $this->getDataCollection(); |
| 87 | return $dataCollection !== null && $dataCollection->shouldCollectDatabaseQueryData(); |
| 88 | } |
| 89 | private function calculateMaxHttpBodyLength(HttpMessageType $messageType) : int |
| 90 | { |
| 91 | if ($this->options === null) { |
| 92 | return 0; |
| 93 | } |
| 94 | if ($messageType->isRequest()) { |
| 95 | $maxRequestBodySize = $this->options->getMaxRequestBodySize(); |
| 96 | // Preserve legacy behaviour to capture request bodies unbounded |
| 97 | if ($this->isLegacyMode() && $maxRequestBodySize === 'always') { |
| 98 | return -1; |
| 99 | } |
| 100 | return self::MAX_REQUEST_BODY_LENGTH[$maxRequestBodySize] ?? 0; |
| 101 | } |
| 102 | return self::MAX_HTTP_BODY_LENGTH; |
| 103 | } |
| 104 | } |
| 105 |